我使用MYSQL Server作为我的项目后端.我有一个DataGridView,它从数据库中填充数据.当我在DataGridView单元格中进行更改并单击saveButton时,数据需要在DataGridView和数据库表中进行更改.
在这里我的编码:
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"); } } }
我有这样的错误(附加信息:对于不返回任何键列信息的SelectCommand,不支持UpdateCommand的动态SQL生成.)行mydtadp.Update(datatab); 如何才能做到这一点?