将数据网格视图中的数据保存到 MySQL
我是使用 Visual Studio 2010 C# 创建应用程序的新手。我正在创建一个应用程序,用户将在 C# 的数据网格视图中输入数据,并自动将其保存在 MySQL 中。
我有这段代码来保存文本框中的数据:
private void buttonSaveEmployee_Click(object sender, EventArgs e)
{
string MyConString = "SERVER=localhost;" + "DATABASE=payroll;" + "UID=root;" + "PASSWORD=admin;";
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
command.Connection = connection;
using (MySqlConnection conn = new MySqlConnection(MyConString))
{
connection.Open();
using (MySqlCommand com = connection.CreateCommand())
{
command.CommandText = "insert into employee(employee_lastname) values(?employee_lastname)";
command.Parameters.Add(new MySqlParameter("?employee_lastname", MySqlDbType.VarChar));
command.Parameters["?employee_lastname"].Value = textBoxEmpLastName.Text;
command.ExecuteNonQuery();
}
}
}
我想知道这是否是从文本框中保存数据的代码,如何将数据从数据网格视图保存到MySQL。任何帮助将不胜感激。谢谢。
I am new in creating application using Visual Studio 2010 C#. I am creating an application where the user will input data in a data grid view in C# and automatically save it in MySQL.
I have this code to save the data from a textbox:
private void buttonSaveEmployee_Click(object sender, EventArgs e)
{
string MyConString = "SERVER=localhost;" + "DATABASE=payroll;" + "UID=root;" + "PASSWORD=admin;";
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
command.Connection = connection;
using (MySqlConnection conn = new MySqlConnection(MyConString))
{
connection.Open();
using (MySqlCommand com = connection.CreateCommand())
{
command.CommandText = "insert into employee(employee_lastname) values(?employee_lastname)";
command.Parameters.Add(new MySqlParameter("?employee_lastname", MySqlDbType.VarChar));
command.Parameters["?employee_lastname"].Value = textBoxEmpLastName.Text;
command.ExecuteNonQuery();
}
}
}
I am wondering if this is the code to save a data from a textbox, how can I save the data from data grid view to MySQL. Any help will be much appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该通过其 DataSource 属性将数据网格绑定到数据库,这样对网格所做的任何更改都将反映在数据库中。
示例
所以这就像调用一样简单
You should bind your datagrid to the database thru its DataSource property this way any changes done to the grid will be reflected in the database.
Example
So it would be as easy as calling