如何使用MYSQL在C#中的datagridview中插入,删除,选择,更新值 [英] How to insert,delete,select,update values in datagridview in C# using MYSQL
本文介绍了如何使用MYSQL在C#中的datagridview中插入,删除,选择,更新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是使用datagridview将值插入到mysql数据库中的代码。
但是selectcommand正在工作。它不会发生,因为我收到错误说明列用户名不能为空。
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 ".
如果我使用ms访问数据库,则不会弹出此错误。
任何人都可以帮助我。有没有其他方法可以这样做。
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.
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);
}
推荐答案
我是相当不确定,但是mysql是否使用带有?的编号参数 - synatx或带有:语法的命名参数,而不是(mssql)语法?
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 ?
某些东西:
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");
或
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");
这篇关于如何使用MYSQL在C#中的datagridview中插入,删除,选择,更新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文