没有为行更新和行删除更新SQLServer数据库 [英] SQLServer database not getting updated for row update and row delete

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

问题描述

我使用的是SQL Server 2005和VS2008。我使用了4个文本框来显示我创建的数据库的4列数据。我正在使用dataadapter和数据集来检索数据库并在文本框中显示。



I am using SQL server 2005 and VS2008. I have used 4 text boxes to show the data from 4 columns of the database I have created. I am using dataadapter and dataset to retrieve the database and show in the textboxes.

con1 = new SqlConnection("Server=(local); uid=sa; pwd=123; database=ado");
     da1 = new SqlDataAdapter("Select * from adotable", con1);
     da1.MissingSchemaAction = MissingSchemaAction.AddWithKey;
     ds1 = new DataSet();
     da1.Fill(ds1, "peple"); 
     textBox1.Text = ds1.Tables["peple"].Rows[rownum][0].ToString();
     textBox2.Text = ds1.Tables["peple"].Rows[rownum][1].ToString();
     textBox3.Text = ds1.Tables["peple"].Rows[rownum][2].ToString();
     textBox4.Text = ds1.Tables[0].Rows[rownum][3].ToString(); 



当我尝试在数据集中插入新值或更新行或删除数据集中的行时,它可以正常工作。

当我尝试更新数据库时,它适用于插入,但数据库没有更新数据集中的删除行和更新行。

以下是分别插入,更新,删除的代码:


When I try to insert a new value in the dataset or update a row or delete a row in the dataset it works fine.
When I try to update the database it works fine for insert but the database doesn't get updated for delete row and update row in dataset.
The following is the code for insert, update, delete respectively:

private void button6_Click(object sender, EventArgs e)
{
     DataRow datrow = ds1.Tables[0].NewRow();
     datrow[0] = textBox1.Text;
     datrow[1] = textBox2.Text;
     datrow[2] = textBox3.Text;
     datrow[3] = textBox4.Text;
     ds1.Tables[0].Rows.Add(datrow);
     MessageBox.Show("Row added to datatable");
}
private void button7_Click(object sender, EventArgs e)
{
     ds1.Tables[0].Rows[rownum][0] = textBox1.Text;
     ds1.Tables[0].Rows[rownum][1] = textBox2.Text;
     ds1.Tables[0].Rows[rownum][2] = textBox3.Text;
     ds1.Tables[0].Rows[rownum][3] = textBox4.Text;
     ds1.AcceptChanges();
     MessageBox.Show("Row updated");
     button1.PerformClick();
}
private void button8_Click(object sender, EventArgs e)
{
     ds1.Tables[0].Rows[rownum].Delete();
     ds1.AcceptChanges();
     MessageBox.Show("Row deleted");
}





更新我正在使用的数据库:



For updating the database I am using :

private void button9_Click(object sender, EventArgs e) // This doesn't work for deleted row or //updated row. This works fine for inserted row. 
{
     SqlCommandBuilder cb1 = new SqlCommandBuilder(da1);
     da1.Update(ds1, "peple");
} 





请帮助



Please help

推荐答案

AcceptChanges [ ^ ]用于清除任何客户端进行的更改(更新/删除),同时仍然在客户。这意味着更新/新行的状态将从已修改/已添加更改为未更改,已删除的行将从数据集中删除。当您在数据集上调用Update时,它将找不到任何更改,因此不会向SQL发送任何内容...
AcceptChanges[^] used to clear any client made changes (update/delete) while still on the client. It means that status of updated/new rows will change from 'Modified'/'Added' to 'Unchanged' and deleted rows will be removed from the data set. When you call Update on your data set it will find no changes and for that will not send nothing to SQL...


此处您只更新数据适配器。在页面上创建一个Update命令,并将dataadapter值传递给更新查询。
Here you are updating the data adapter only. Create an Update command on the page and pass the dataadapter value to the update query.


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

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