我在C#Visual Studio 2012中有一个简单的应用程序,它包含一个显示SQL表记录的数据网格。 我添加了文本框和组合框来添加新记录,这很好。
我想要做的是直接在数据网格中更新现有记录。 我有更新查询的按钮,但我有一个问题。 如何识别已修改的网格中的记录?
Lests说网格显示
ID Name Age Gender ---- -------- ---- -------- 1 Steven 21 M 2 Dan 34 M此数据显示使用
SELECT * FROM table.我进入网格,我将第二条记录的年龄从43修改为36.要保存它我需要运行更新查询,但我怎么能告诉我修改的ID是2? 有什么方法可以做到这一点吗? 还是其他任何方式?
编辑:
它像这样工作来更新数据网格:
SqlConnection con = new SqlConnection("user id=testuser12;" + "password=Reporting11#;Data Source=SERVER;" + // "Trusted_Connection=yes;" + "Initial Catalog=Partner_database; " + "connection timeout=30"); SqlCommand command = new SqlCommand("select * from [dbo].[Test_table]", con) SqlDataAdapter sda = new SqlDataAdapter(command); DataSet set = new DataSet("cucu"); sda.Fill(set,"cucu"); dataGridView1.DataSource = set; dataGridView1.DataMember = "cucu";我现在正在测试看看如何做DataSet.AcceptChanges(); 部分,因为如果我使用按钮我如何调用数据集? 因为数据集在代码的另一部分。
编辑2:我试着写这个回答,但我的答案被删除,没有解释,所以我被迫在这里做另一个编辑。
我已经尝试过你所说的但它似乎不起作用。 我有数据网格显示数据,但是当我修改现有报告并单击SAVE时,我收到一个错误:“当传递带有修改行的DataRow集合时,更新需要有效的UpdateCommand。”
保存按钮代码是:
private void test_tableBindingNavigatorSaveItem_Click(object sender, EventArgs e) { this.Validate(); this.test_tableBindingSource.EndEdit(); this.tableAdapterManager.UpdateAll(this.partner_databaseDataSet); }而fillby函数是:
private void fillByToolStripButton_Click(object sender, EventArgs e) { try { this.test_tableTableAdapter.FillBy(this.partner_databaseDataSet.Test_table, valueToolStripTextBox.Text); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } }保存时,错误显示在以下行:
this.tableAdapterManager.UpdateAll(this.partner_databaseDataSet);我是否需要修改保存按钮的功能? 没有“.AcceptChanges”。
I have a simple app in C# Visual Studio 2012 that contains a Data Grid showing the records from an SQL table. I have added the text boxes and combo boxes to add new records, that works just fine.
What i am trying to do is update an existing record directly in the data grid. I have the button with the update query but i have one problem. How do i identify the record in the grid that was modified?
Lests say the grid shows
ID Name Age Gender ---- -------- ---- -------- 1 Steven 21 M 2 Dan 34 MThis data is shown using
SELECT * FROM table.I go into the grid and i modify the Age for the second record from 43 to 36. To save it i need to run an update query, but how can i tell that the ID that was modified was 2? Is there any way i can do this? Or any other way?
EDIT:
It works like this to update the datagrid:
SqlConnection con = new SqlConnection("user id=testuser12;" + "password=Reporting11#;Data Source=SERVER;" + // "Trusted_Connection=yes;" + "Initial Catalog=Partner_database; " + "connection timeout=30"); SqlCommand command = new SqlCommand("select * from [dbo].[Test_table]", con) SqlDataAdapter sda = new SqlDataAdapter(command); DataSet set = new DataSet("cucu"); sda.Fill(set,"cucu"); dataGridView1.DataSource = set; dataGridView1.DataMember = "cucu";I am now testing to see how to do do the DataSet.AcceptChanges(); part, because if i use a button how do i call the dataset? because the dataset is in another part of the code.
EDIT2: I tried to write this into an answer but my answer was deleted with no explanation so i am forced to do another edit here.
I have tried what you said but it does not seem to work. I have the Data grid showing the data but when i modify an existing report and click SAVE i get an error:"Update requires a valid UpdateCommand when passed DataRow collection with modified rows."
the save button code is:
private void test_tableBindingNavigatorSaveItem_Click(object sender, EventArgs e) { this.Validate(); this.test_tableBindingSource.EndEdit(); this.tableAdapterManager.UpdateAll(this.partner_databaseDataSet); }And the fillby function is:
private void fillByToolStripButton_Click(object sender, EventArgs e) { try { this.test_tableTableAdapter.FillBy(this.partner_databaseDataSet.Test_table, valueToolStripTextBox.Text); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } }On save the error shows on the following line:
this.tableAdapterManager.UpdateAll(this.partner_databaseDataSet);Do i need to modify what the save button is doing? There is no ".AcceptChanges" .
最满意答案
我把另一个答案留给了简短的参考。 请先关注示例项目中的“教程”,以便全面了解它是什么以及如何使用它。 在某些地方听起来可能有些基本但对使用SO的其他人有好处。
1)创建一个新的WinFom应用程序
在Visual Studio上,单击“ FILE -> New -> Project 。 选择Windows Forms Application ,为其命名或保留默认值,然后单击“ OK 。
2)添加数据库连接
单击VIEW -> Server Explorer菜单或只需使用Ctrl + W , L键盘组合。 (这是按住Ctrl并按W然后按L )
在打开的面板上,单击“ Connect to Database按钮。
填写必填字段,测试连接并单击“ OK
3)创建数据源
单击VIEW -> Other Windows -> Data Sources菜单或只需使用Shift + Alt + D键盘组合。
在打开的面板上,单击“ Add New Data Source按钮。
在第一个屏幕中选择Database ,在第二个屏幕中选择Dataset ,然后单击Next 。 从下拉列表中选择刚刚创建的数据连接,然后再次单击“ Next两次。
在数据库对象选择屏幕中,选择要包含在数据集中的表,然后单击“ Finish 。
4)将DataGrid添加到窗体
在“ Data Sources面板中,单击您之前选择的其中一个对象,然后选择“ DataGridView
接下来,将对象名称拖放到表单中。 将生成一些控件(包括数据网格)。 将使用所需的连接,绑定和数据源设置数据网格。
5)浏览代码
尝试通过生成的代码和控件属性来更好地理解解决方案。 请注意表单中生成的以下代码行:
private void Form1_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'testDataSet.Language' table. You can move, or remove it, as needed. this.languageTableAdapter.Fill(this.testDataSet.Language); } private void languageBindingNavigatorSaveItem_Click(object sender, EventArgs e) { this.Validate(); this.languageBindingSource.EndEdit(); this.tableAdapterManager.UpdateAll(this.testDataSet); }为对象生成了一个TableAdapter ,并在加载表单时填充了testDataSet数据。
Save按钮(小软盘图标)正在调用tableAdapterManager.UpdateAll(this.testDataSet) ,它将隐式调用DataSet.AcceptChanges() 。
请注意,您可以通过更改选择查询来缩小数据集的范围(通过单击上一屏幕中的“ Add Query ”)。
6)创建参数化查询 - 编辑
您可以在给定数据集的海关查询中使用参数。 您可以在您的情况下使用以下查询:
SELECT * FROM Table WHERE (Id = @Value)ADO.NET将生成一个新的FillBy()方法,您可以使用它来代替Form_Load方法中的常规Fill() 。 生成的方法将具有类似于以下的签名:
public virtual int FillBy(TestDataSet.TableDataTable dataTable, int Value)请注意,输入参数的数量将扩展您拥有的查询参数的数量。 该类型将匹配目标数据库中字段的类型(对于Id,为hete int )
@Value对SQL Server有效。 如果您正在使用其他DBMS ? 代替
SELECT * FROM Table WHERE (Id = ?)I managed to solve this declaring public variables :
private string variabila; private int varsta; private DataSet set = new DataSet("cucu"); private SqlDataAdapter sda; private SqlCommandBuilder cmdBuilder;And using the SQLCommandBuilder for the update:
private void button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Data Source=SERVER;" + "Initial Catalog=Partner_database;" + "Integrated Security=True"); SqlCommand command = new SqlCommand("select * from [dbo].[Test_table]", con); sda = new SqlDataAdapter(command); sda.AcceptChangesDuringFill = true; sda.AcceptChangesDuringUpdate = true; set.Clear(); //just to make sure i have a clear set cmdBuilder = new SqlCommandBuilder(sda); //use the command builder to create the commands sda.Fill(set_date,"cucu1"); dataGridView1.DataSource = set_date; dataGridView1.DataMember = "cucu1"; }And to save the updated records into the database:
private void button4_Click(object sender, EventArgs e) { sda.Update(set.Tables["cucu"]); }The Update command is not working without the Command Builder.
更多推荐
发布评论