如何使用MYSQL在C#中的datagridview中插入、删除、选择、更新值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1518946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert,delete,select,update values in datagridview in C# using MYSQL
提问by Srikanth V M
Below is the code to insert value into mysql database, using datagridview. But the selectcommand is working.It is not happening since i get error stating "Column 'username' cannot be null ".
下面是使用datagridview将值插入mysql数据库的代码。但是select命令正在工作。它没有发生,因为我收到错误提示“列'用户名'不能为空”。
This error does not pop up if i use ms access database. Can anyone help me on this. is there any other method to do so.
如果我使用 ms access 数据库,则不会弹出此错误。谁可以帮我这个事。有没有其他方法可以做到这一点。
private void button1_Click(object sender, EventArgs e)
{ //Even using functions we can easily update the datagridview
//Select_function();
try
{ //The container which displays the details.
dataGridView1.Visible = true;
//The binding object which binds the datagridview with backend.
BindingSource bs = new BindingSource();
//The datatable through which data is exported to datagridview
table = new DataTable();
bs.DataSource = table;
this.dataGridView1.DataSource = bs;
MySqlConnection conn = new MySqlConnection(db);
conn.Open();
string s = "select *from user";
cmd = new MySqlCommand(s, conn);
da = new MySqlDataAdapter();
da.SelectCommand = new MySqlCommand(s, conn);
//There is issue in below sytax of insert command.
MySqlCommand insertcommand = new MySqlCommand("insert into user(username,password) values(@username ,@password)", conn);
insertcommand.Parameters.Add("username",MySqlDbType.VarChar,50,"username");
insertcommand.Parameters.Add("password", MySqlDbType.VarChar, 50, "password");
da.InsertCommand = insertcommand;
//Demonstrates update command
MySqlCommand updatecommand = new MySqlCommand("update user set username=@username,password=@password where (username=@username)", conn);
updatecommand.Parameters.Add("@username", MySqlDbType.VarChar, 50, "username");
updatecommand.Parameters.Add("@password", MySqlDbType.VarChar, 50, "password");
da.UpdateCommand = updatecommand;
//Demonstration of delete Command
MySqlCommand deletecommand = new MySqlCommand("delete from user where username=@username", conn);
deletecommand.Parameters.Add("@username", MySqlDbType.VarChar, 50, "username");
da.DeleteCommand = deletecommand;
da.Fill(table);
conn.Close();
}
catch (Exception err) { MessageBox.Show(err.Message); }
}
private void button2_Click(object sender, EventArgs e)
{ da.Update(table);
}
采纳答案by Nils
I am rather unsure, but doesn't mysql use numbered parameters with a "?"-synatx or named parameters with a ":"-syntax instead of (mssql) at-syntax ?
我相当不确定,但 mysql 不使用带有“?”-syntax 的编号参数或带有“:”-syntax 的命名参数而不是 (mssql) at-syntax 吗?
Something like :
就像是 :
MySqlCommand insertcommand = new MySqlCommand("insert into user(username,password) values(?, ?)", conn);
insertcommand.Parameters.Add(1, MySqlDbType.VarChar,50,"username");
insertcommand.Parameters.Add(2, MySqlDbType.VarChar, 50, "password");
or
或者
MySqlCommand insertcommand = new MySqlCommand("insert into user(username,password) values(:username, :pass)", conn);
insertcommand.Parameters.Add(":username", MySqlDbType.VarChar,50,"username");
insertcommand.Parameters.Add(":pass", MySqlDbType.VarChar, 50, "password");
回答by dariom
I think you need to change the names of the parameters added to the insert command to match the names specified in the INSERT SQL statement.
我认为您需要更改添加到插入命令的参数名称以匹配 INSERT SQL 语句中指定的名称。
Try:
尝试:
insertcommand.Parameters.Add("@username",MySqlDbType.VarChar,50,"username");
insertcommand.Parameters.Add("@password", MySqlDbType.VarChar, 50, "password");
回答by LiTuNi
Had a similar problem updating in mysql with datagrid (vb.net). Solved it with:
在使用 datagrid (vb.net) 更新 mysql 时遇到了类似的问题。解决了它:
Dim cmb As New MySqlCommandBuilder(datGrid)
Me.datGrid.Update(datSet, "mysqlTableToUpdate")