2013-02-14 235 views
0

我正在使用Visual Studio C#2010 Express,並在使用mySQL數據庫時遇到一些問題。C#MySQL插入,刪除,更新按鈕?

我已經實現了連接按鈕,以便在填寫完所有帳戶信息(服務器,用戶,端口和密碼)後,單擊連接按鈕,然後會顯示數據庫列表以及每個數據庫中的表格當你選擇某個數據庫時),以及每個表中的數據(當你選擇某個表時)。

現在我試圖實現插入,更新和刪除按鈕,但不知道從哪裏開始,我大多對插入按鈕(我的意思是不同的表具有不同數量的列,所以...)和混淆和當單擊插入按鈕時,我無法想象它是如何工作的。有人可以給我一些建議或示例代碼,以便我可以跟隨其餘的。

謝謝!

回答

1

如果你能找到一種方法來輸入新記錄到網格的最後一排,你可以制定一個查詢,像這樣

cmd.CommandText = "insert into " + listTables.Items[listTables.SelectedIndex].ToString() + "("; 

foreach(var col in dataGridTableView.DataSource.DataTable[0].Columns) 
    cmd.CommandText += col.ColumnName + "," ; 

// trim last comma off 

cmd.CommandText += ") Values("; 

// access newly created row 
// iterate over the cells of the row, and add values to the command text we've been building 
// execute command text. 
+0

是的,我的最後一列中輸入單詞網格,你能提供一個詳細的代碼和評論,所以我可以理解你在做什麼?真的很感激它。 – 2013-02-15 06:04:26

0

你也許可以看看下面的內容。 http://www.daniweb.com/software-development/csharp/threads/235966/mysql-insert-from-c-app

public void CreateMySqlCommand() 
{ 
    MySqlConnection myConnection = new MySqlConnection("Persist Security Info=False;database=test;server=myServer"); 
    myConnection.Open(); 
    MySqlTransaction myTrans = myConnection.BeginTransaction(); 
    string mySelectQuery = "SELECT * FROM myTable"; 
    MySqlCommand myCommand = new MySqlCommand(mySelectQuery, myConnection,myTrans); 
    myCommand.CommandTimeout = 20; 
} 

我將利用命令的一些SQL做我的插入/更新/刪除等

見MySQL的SQL語法 http://dev.mysql.com/doc/refman/5.0/en/sql-syntax.html

轉到以下:13.2。數據操作語句