2011-08-09 31 views
-2

請讓我知道如何插入兩個變量。它不直接給我的用戶名和移動中的代碼一樣我的C#代碼插入在mysql中的問題

string insert [email protected]"Insert into userHistory(userid,mobile) values(x,y)"; 

一個問題這是我的代碼,但它未能插入(編者注:OP提供錯誤)

int userid = 123456; 
long mobile = 91888888888; 
sqlConn = new MySqlConnection(/* conn string removed */); 
sqlConn.Open(); 
string insert = 
@"Insert into userHistory(userid,mobile) values(@userid,@mobile);"; 

MySqlCommand cmd = new MySqlCommand(insert,sqlConn);  
cmd.CommandType = CommandType.StoredProcedure;  
cmd.Parameters.Add(new MySqlParameter("@userid", userid)); 
cmd.Parameters.Add(new MySqlParameter("@mobile", mobile)); 
+3

「我無法做到」並不真正描述問題。見http://tinyurl.com/so-hints –

+0

什麼是錯誤? –

+2

嘿@Pranay Rana,我認爲你的'?'-key卡住了;-) –

回答

4

無更多的信息,我猜你的命令的類型應該是「文本」,而不是CommandType.StoredProcedure,因爲你不執行SP。而且,你必須執行命令(也許這只是在你的代碼失蹤)

+0

我嘗試過使用cmd.CommandType = CommandType.Text;它沒有插入我的表中 – Raj

+0

你執行了命令嗎?我不知道確切的API,但像cmd.execute()或cmd.ExecuteNonQuery()應該這樣做。你有什麼錯誤嗎? –

+0

謝謝。它的cmd.ExecuteNonQuery()。 – Raj

1

這是錯誤的cmd.CommandType = CommandType.StoredProcedure的命令應該是CommandType.Text

你可以閱讀更多有關CommandType enumeration here

還有一些更.NET examples for MySql here

+0

我試過用cmd.CommandType = CommandType.Text;它沒有插入我的桌子。 – Raj

1

我認爲這個問題是cmd.CommandType = CommandType.StoredProcedure;

您正在嘗試執行即席查詢,不是一個存儲過程。嘗試將其更改爲

cmd.CommandType = CommandType.Text;  
+0

我嘗試過使用cmd.CommandType = CommandType.Text;它沒有插入我的桌子。 – Raj