【问题标题】:Updating a SQL database from a Datagridview data: Dynamic SQL generation for the UpdateCommand is not supported从 Datagridview 数据更新 SQL 数据库:不支持为 UpdateCommand 生成动态 SQL
【发布时间】:2019-10-04 04:09:16
【问题描述】:

我的数据库中有一个名为“TB1”的表。当我更改其数据并单击更新button 时,我想从dataGridview 控件更新我的SQL 数据库,但出现错误。

错误是:

System.InvalidOperationException: Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information. 
at System.Data.Common.DbDataAdapter.UpdatingRowStatusErrors(RowUpdatingEventArgs rowUpdatedEvent, DataRow dataRow) 
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping) at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping) 
at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)

这有什么问题?

> public partial class Form1 : Form
>     {
> 
>         SqlConnection con;
>         SqlDataAdapter adapt;
>         DataSet ds;
>         SqlCommandBuilder cmdbl;
> 
>         public Form1()
>         {
>             InitializeComponent();
>      
>         }
>          private void Form1_Load(object sender, EventArgs e)
>         {
>             con = new SqlConnection();
>             con.ConnectionString= (@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=H:\New\RemDaBase.mdf;Integrated
> Security=True;Connect Timeout=30");
>             con.Open();
>             adapt = new SqlDataAdapter("SELECT * FROM  TB1", con);
>             ds = new DataSet();
>             adapt.Fill(ds, "TB1");
>             dataGridView1.DataSource = ds.Tables[0];
>         }
> 
>  private void button1_Click(object sender, EventArgs e)
>         {
>             try
>             {
>                 cmdbl = new SqlCommandBuilder(adapt);
>                 adapt.Update(ds, "TB1");
>                 MessageBox.Show("Updated Successfully");
>             }
>             catch (Exception ex)
>             {
>                 MessageBox.Show(ex.ToString());
> 
>             }
>         }
>     }

【问题讨论】:

  • 顺便说一句。您的连接似乎永远不会关闭。
  • 但是当我关闭它时它也不起作用。
  • 关闭连接将无法正常工作。什么错误?
  • 我的意思是还有之前的错误。
  • 上一个错误是什么。 “它不起作用”不够具体:)

标签: c# sql database datagridview


【解决方案1】:

你可以这样使用它。

private void Form1_Load(object sender, EventArgs e)
     {

         con = new SqlConnection();
         con.ConnectionString= (@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=H:\New\RemDaBase.mdf;Integrated Security=True;Connect Timeout=30");
         con.Open();
         adapt = new SqlDataAdapter("SELECT * FROM  TB1", con);
         DataTable dt = new DataTable();
         BindingSource bs = new BindingSource();
         bs.DataSource = dt;
         adapt.Fill(dt);
         dataGridView1.DataSource = dt;
     }

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        cmdbl = new SqlCommandBuilder(adapt);
        adapt.Update(dt);
        MessageBox.Show("Updated Successfully");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());

    }
}

【讨论】:

  • 它说更新成功。但它不会更新数据库表。
  • 同样的错误。
【解决方案2】:

哦,我找到了解决办法。

这是因为我没有在我的数据库中使用primary key

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 2018-06-01
    • 2019-03-25
    • 1970-01-01
    相关资源
    最近更新 更多