2012-08-06 64 views
0

我在使用.mdb数据库的asp.net项目中执行存储过程时遇到问题。我想用存储过程,但之后executnion代码...执行后的预期查询名称

using (OleDbConnection conn = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Database"].ConnectionString.ToString())) 
       { 
        conn.Open(); 
        using (OleDbCommand com = new OleDbCommand("Insert", conn)) 
        { 
         com.CommandType = CommandType.StoredProcedure; 
         com.Parameters.AddWithValue("@Login", UserName0.Text); 
         com.Parameters.AddWithValue("@Password", Hashing.Hash(ConfirmPassword0.Text)); 
         com.Parameters.AddWithValue("@Role", RoleList1.SelectedValue); 
         com.ExecuteNonQuery(); 
        } 
        conn.Close(); 

我有例外:

System.Data.OleDb.OleDbException:预计查询名称后EXECUTE。

但是,当我使用下面的代码一切都很好。 我也改变了:CommandType.StoredProcedure CommandType.Text,但它仍然无法正常工作。有人能帮助我吗?

using (OleDbConnection conn = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Database"].ConnectionString.ToString())) 
       { 
        conn.Open(); 
        using (OleDbCommand com = new OleDbCommand("INSERT INTO Workers (st_login, st_password, st_role) VALUES (login, password, role);", conn)) 
        { 
         com.Parameters.AddWithValue("@Login", UserName0.Text); 
         com.Parameters.AddWithValue("@Password", Hashing.Hash(ConfirmPassword0.Text)); 
         com.Parameters.AddWithValue("@Role", RoleList1.SelectedValue); 
         com.ExecuteNonQuery(); 
        } 
        conn.Close(); 
+2

您有一个名为'Insert'的存储过程? – JustinStolle 2012-08-06 10:38:26

+0

对于在querry中的每个参数名称之前使用参数add @的简单插入:INSERT INTO Workers(st_login,st_password,st_role)VALUES(@Login,@Password,@Role); – JleruOHeP 2012-08-06 10:41:01

回答

1

如果你的代码清单是准确的,它似乎有一个名为Insert一个存储过程,而不能是可取的。尝试使用不是保留关键字的名称创建过程,看看是否有帮助。

+0

它帮助和解决了问题。感谢 – user1578754 2012-08-06 10:46:33

0
using (OleDbConnection conn = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Database"].ConnectionString.ToString())) 
       { 
        conn.Open(); 
        using (OleDbCommand com = new OleDbCommand("INSERT INTO Workers (st_login, st_password, st_role) VALUES (?, ?, ?);", conn)) 
        { 
         com.Parameters.AddWithValue("@Login", UserName0.Text); 
         com.Parameters.AddWithValue("@Password", Hashing.Hash(ConfirmPassword0.Text)); 
         com.Parameters.AddWithValue("@Role", RoleList1.SelectedValue); 
         com.ExecuteNonQuery(); 
        } 
        conn.Close(); 

请注意Ms-Access不支持存储过程。如果您需要如此复杂的查询,请访问Sql Server。因为你的应用程序正在使用asp.net。我会推荐ASP.Net与Sql Server作为后端。反正你的答案,用参数替换?当使用MS-Access时。往上看。

+0

你觉得'@ Login'在你有'?'时有意义吗? – V4Vendetta 2012-08-06 10:44:17

+0

是的!!它会工作... – 2012-08-06 10:45:42

+0

@ V4Vendetta:与同样的情况下的另一个问题... http://stackoverflow.com/questions/10941284/how-to-insert-a-record-into-a- access-table-using-oledb – 2012-08-06 10:47:06