编辑DataGridview并使用c#将其保存在数据库表中 [英] To Edit the DataGridview and also save it in a database table using c#
问题描述
这里我的代码:
使用MySql.Data.MySqlClient;
使用系统;
使用System.Data;
使用System.IO;
使用System.Windows.Forms;
命名空间datagridview
{
public partial class Form1:Form
{
DataTable数据;
MySqlDataAdapter mydtadp;
MySqlCommandBuilder cmbl;
String MyConnection =SERVER = *****; +
DATABASE = ****; +
UID = root; +
PASSWORD = pws;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender,EventArgs e)
{
MySqlConnection MyConn = new MySqlConnection MyConnection);
MyConn.Open();
MySqlCommand comand = new MySqlCommand(select * from aster_scripts;,MyConn);
datatab = new DataTable();
mydtadp = new MySqlDataAdapter(comand);
mydtadp.Fill(datatab);
dataGridView1.DataSource = datatab;
MyConn.Close();
}
private void BtnSave_Click(object sender,EventArgs e)
{
DataSet ds = new DataSet );
cmbl = new MySqlCommandBuilder(mydtadp);
mydtadp.Update(datatab);
MessageBox.Show(SAVED);
}
}
}
我有这样的错误(附加信息:UpdateCommand的动态SQL生成对于不返回任何键列信息的SelectCommand不支持。)mydtadp.Update(datatab);
如何做到这一点?
请注意模块级变量。并参考这个MSDN作品
数据库条目
mysql> select * from aster_scripts;
+ ---- + ---------- + -------- +
| id |我| sThing |
+ ---- + ---------- + -------- +
| 1 | 8 |青蛙|
| 2 | 11333322 |猫|
+ ---- + ---------- + -------- +
I use MYSQL Server as my project backend.I have a DataGridView that gets filled with data from the database. when i make changes in the DataGridView cells and i click a saveButton the data need to change in DataGridView and also in database table.
here my coding:
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.IO;
using System.Windows.Forms;
namespace datagridview
{
public partial class Form1 : Form
{
DataTable datatab;
MySqlDataAdapter mydtadp;
MySqlCommandBuilder cmbl;
String MyConnection = "SERVER=*****;" +
"DATABASE=****;" +
"UID=root;" +
"PASSWORD=pws";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MySqlConnection MyConn = new MySqlConnection(MyConnection);
MyConn.Open();
MySqlCommand comand = new MySqlCommand("select * from aster_scripts;", MyConn);
datatab = new DataTable();
mydtadp = new MySqlDataAdapter(comand);
mydtadp.Fill(datatab);
dataGridView1.DataSource = datatab;
MyConn.Close();
}
private void BtnSave_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
cmbl = new MySqlCommandBuilder(mydtadp);
mydtadp.Update(datatab);
MessageBox.Show("SAVED");
}
}
}
i had a error like this (Additional information: Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information.) in line mydtadp.Update(datatab); How can this be done?
Please notice the module-level variables. And reference this MSDN piece Here if need be.
schema
create table aster_scripts
( id int auto_increment primary key,
i int not null,
sThing varchar(30) not null
);
insert aster_scripts (i,sThing) values (8,'frog'),(11,'cat');
c#
using System;
using System.Data;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
private MySqlDataAdapter mydtadp = new MySqlDataAdapter();
private BindingSource bindingSource1 = new BindingSource();
MySqlCommandBuilder cmbl;
String MyConnection = "SERVER=hostname;" +
"DATABASE=dbname;" +
"UID=dbuser;" +
"PASSWORD=fffff";
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
MySqlConnection MyConn = new MySqlConnection(MyConnection);
MyConn.Open();
mydtadp.SelectCommand=new MySqlCommand("select * from aster_scripts", MyConn);
cmbl = new MySqlCommandBuilder(mydtadp);
DataTable table = new DataTable();
mydtadp.Fill(table);
bindingSource1.DataSource = table;
dataGridView1.DataSource = bindingSource1;
}
private void BtnSave_Click(object sender, EventArgs e)
{
mydtadp.Update((DataTable)bindingSource1.DataSource);
MessageBox.Show("SAVED");
}
}
}
Chg data hit save Screen Shot
DB Entries
mysql> select * from aster_scripts;
+----+----------+--------+
| id | i | sThing |
+----+----------+--------+
| 1 | 8 | frog |
| 2 | 11333322 | cat |
+----+----------+--------+
这篇关于编辑DataGridview并使用c#将其保存在数据库表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!