的WinForms DataGridView的 - 更新数据库 [英] WinForms DataGridView - update database

查看:181
本文介绍了的WinForms DataGridView的 - 更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是在DataGridView的基本功能,但由于某些原因,我不能得到它的工作。我只是想在我的Windows窗体DataGridView的向它提交作出当用户点击保存按钮,对数据库的任何更改。

I know this is a basic function of the DataGridView, but for some reason, I just can't get it to work. I just want the DataGridView on my Windows form to submit any changes made to it to the database when the user clicks the "Save" button.

予根据由用户选择在一个DropDownList触发的功能填充在DataGridView如下:

I populate the DataGridView according to a function triggered by a user selection in a DropDownList as follows:

using (SqlConnection con = new SqlConnection(conString))
{
    con.Open();
    SqlDataAdapter ruleTableDA = new SqlDataAdapter("SELECT rule.fldFluteType AS [Flute], rule.fldKnife AS [Knife], rule.fldScore AS [Score], rule.fldLowKnife AS [Low Knife], rule.fldMatrixScore AS [Matrix Score], rule.fldMatrix AS [Matrix] FROM   dbo.tblRuleTypes rule WHERE rule.fldMachine_ID = '1003'", con);
    DataSet ruleTableDS = new DataSet();
    ruleTableDA.Fill(ruleTableDS);
    RuleTable.DataSource = ruleTableDS.Tables[0];
}

在我保存功能,我主要有以下(我已经整理出了一些code围绕它去了吧):

In my save function, I basically have the following (I've trimmed out some of the code around it to get to the point):

using (SqlDataAdapter ruleTableDA = new SqlDataAdapter("SELECT rule.fldFluteType AS [Flute], rule.fldKnife AS [Knife], 
       rule.fldScore AS [Score], rule.fldLowKnife AS [Low Knife],
       rule.fldMatrixScore AS [Matrix Score], rule.fldMatrix AS [Matrix]
       FROM dbo.tblRuleTypes rule WHERE rule.fldMachine_ID = '1003'", con))
    {
        SqlCommandBuilder commandBuilder = new SqlCommandBuilder(ruleTableDA);
        DataTable dt = new DataTable();
        dt = RuleTable.DataSource as DataTable;
        ruleTableDA.Fill(dt);
        ruleTableDA.Update(dt);
    }

好了,所以我编辑了code做到以下几点:打造命令,创建基于在DataGridView(RuleTable)一个DataTable,填写DataAdapter的的数据表,并更新数据库。现在ruleTableDA.Update(DT)抛出异常并发冲突:在更新命令影响0预期1条记录

Okay, so I edited the code to do the following: build the commands, create a DataTable based on the DataGridView (RuleTable), fill the DataAdapter with the DataTable, and update the database. Now ruleTableDA.Update(dt) is throwing the exception "Concurrency violation: the UpdateCommand affected 0 of the expected 1 records."

推荐答案

我相信有一些问题在这里: 要记住的顺序是,当你加载你的网格,它是指向一个数据表/套了。 当你输入到电网,改变被暂时保存到绑定到网格中的数据表。因此,你不希望在创建数据表为您节省每一次,因为现有的数据表中所做的更改会被忽略。 第二个问题是,你可能并不需要建立一个绑定源每次,因为就像第一点,如果网格是显示的数据,已经有约束力的来源,它必然。 第三个问题是,SQLCommandBuilder类有像GetInsertCommand,GetUpdateCommand时等必须使用真正得到相应的命令的方法。

I believe there are a few problems here: The sequence to keep in mind is that when you load up your grid, it is pointed to a datatable/set already. When you type into the grid, the changes are temporarily persisted to the data table that is bound to the grid. Therefore, you do not want to be creating a data table every time you save, because the changes that were made to the existing data table are being ignored. Second issue is that you probably don't need to create a binding source everytime, because just like the first point, if the grid is showing data, there is already a binding source that it is bound to. Third problem is that the SQLCommandBuilder class has methods like GetInsertCommand, GetUpdateCommand, etc. that must be used to actually get the appropriate commands.

using (SqlDataAdapter ruleTableDA = new SqlDataAdapter("SELECT rule.fldFluteType AS [Flute], rule.fldKnife AS [Knife], 
       rule.fldScore AS [Score], rule.fldLowKnife AS [Low Knife],
       rule.fldMatrixScore AS [Matrix Score], rule.fldMatrix AS [Matrix]
       FROM dbo.tblRuleTypes rule WHERE rule.fldMachine_ID = '1003'", con))
    {
        SqlCommandBuilder commandBuilder = new SqlCommandBuilder(ruleTableDA);
        DataTable dt = new DataTable();
        dt = RuleTable.DataSource as DataTable;
        //ruleTableDA.Fill(dt);
        ruleTableDA.Update(dt);
    }

这篇关于的WinForms DataGridView的 - 更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆