Windows窗体应用程序中的DataGridview

人气:481 发布:2022-09-22 标签: .net .NET .NET4

问题描述

我的应用程序中有一个Winforms DataGridView 编辑单元格中的数据后,我单击以在数据库中永久更新更改,它会显示错误消息: - 对于不返回任何键列信息的selectCommand,不支持UpdateCommand的动态sql生成。 i无法理解这里的问题。 公共部分类Form1:表格 { 私有SqlDataAdapter da ; 私有SqlConnection conn; BindingSource bsource = new BindingSource(); DataSet ds = null; string sql; public Form1() { InitializeComponent(); } private void LoadData() { string connectionString =data source = rakesh-PC; initial cata log = mysoftdb; user id = sa; password = Ruhil; conn = new SqlConnection(connectionString); sql =select * from customer; da = new SqlDataAdapter(sql,conn); conn.Open(); ds = new DataSet(); SqlCommandBuilder commandBuilder = new SqlCommandBuilder(da); da.Fill(ds,customer); bsource.DataSource = ds.Tables [customer] ; dgv.DataSource = bsource; } private void btnLoad_Click(object sender,EventArgs e) { LoadData(); } private void btnUpdate_Click(对象发送者,EventArgs e) { 尝试 { DataTable dt = ds.Tables [客户]; this.dgv.BindingContext [dt]。 EndCurrentEdit(); this.da.Update(dt); } catch(Exception ex) { MessageBox.Show(ex.Message); } } 谢谢

I have a Winforms DataGridView in my application After editing the data in the cells, i click to update the changes permanently in the database it displays an error message:- "Dynamic sql generation for the UpdateCommand is not supported against a selectCommand that does not return any key column information." i could't understand what's the problem here. public partial class Form1 : Form { private SqlDataAdapter da; private SqlConnection conn; BindingSource bsource = new BindingSource(); DataSet ds = null; string sql; public Form1() { InitializeComponent(); } private void LoadData() { string connectionString = "data source=rakesh-PC;initial catalog=mysoftdb;user id=sa;password=Ruhil"; conn = new SqlConnection(connectionString); sql = "select *from customer"; da = new SqlDataAdapter(sql, conn); conn.Open(); ds = new DataSet(); SqlCommandBuilder commandBuilder = new SqlCommandBuilder(da); da.Fill(ds, "customer"); bsource.DataSource = ds.Tables["customer"]; dgv.DataSource = bsource; } private void btnLoad_Click(object sender, EventArgs e) { LoadData(); } private void btnUpdate_Click(object sender, EventArgs e) { try { DataTable dt = ds.Tables["customer"]; this.dgv.BindingContext[dt].EndCurrentEdit(); this.da.Update(dt); } catch (Exception ex) { MessageBox.Show(ex.Message); } } Thanks

推荐答案

当你收到这样的错误时,第一个要做的就是将整个错误信息提供给Google: Google:对于不返回任何选项的selectCommand,不支持UpdateCommand的动态sql生成关键列信息 [ ^ ] 正如您所看到的,这会返回大约4,000个结果 - 所有这些都与您的确切问题有关。 在这种情况下,我怀疑第一个链接可能是帮助:你有没有为你的桌子设置主键? 如果没有,请在发布之前先看看其他几个 - 它更快,更容易,如果它不能解决您的问题,它会让您知道该告诉我们什么! When you get an error like this, the first thign to do is feed the entire error message to Google: Google: "Dynamic sql generation for the UpdateCommand is not supported against a selectCommand that does not return any key column information"[^] As you can see, that returns about 4,000 results - all concerned with your exact problem. In this case, I suspect the first link will probably help: have you set a Primary key for your table? If it doesn't, have a look at a couple of others before you post here - it's quicker, easier and if it doesn't solve your problem it lets you know what to tell us!

279