2012-08-06 62 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