通过DataAdapter删除行

发布于 2024-10-05 03:08:22 字数 463 浏览 8 评论 0原文

我已经初始化了一个 dataAdapter :

string sql = "SELECT * From localitati";
da1 = new System.Data.SqlClient.SqlDataAdapter(sql, con);
da1.Fill(ds1, "localitati");

这工作得很好。问题是当我尝试删除记录并更新数据库时。 我这样从数据集中删除一条记录:

ds1.Tables["localitati"].Rows.Remove(dRow);

这也很好用(已验证)。

问题是当我更新 DataAdapter 时,数据库没有被修改:

con.Open()
da1.Update(ds1, "localitati");
con.Close();

可能是什么问题?

I've initialized a dataAdapter :

string sql = "SELECT * From localitati";
da1 = new System.Data.SqlClient.SqlDataAdapter(sql, con);
da1.Fill(ds1, "localitati");

And this works just fine. The problem is when i try to delete a record and update the database.
I remove a record from the dataset as such :

ds1.Tables["localitati"].Rows.Remove(dRow);

And this works just fine as well(verified).

The problem is when i update the DataAdapter, the DataBase doesn't get modified :

con.Open()
da1.Update(ds1, "localitati");
con.Close();

What could be the problem ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忆离笙 2024-10-12 03:08:23
DataRow dr = datatable.Rows.Find(key);      
int Index = datatable.Rows.IndexOf(dr);

BindingContext[DataTable].RemoveAt(Index);     
BindingContext[DataTable].EndCurrentEdit(); 

dataAdapter.Update(DataTable);    
DataTable.AcceptChanges();
DataRow dr = datatable.Rows.Find(key);      
int Index = datatable.Rows.IndexOf(dr);

BindingContext[DataTable].RemoveAt(Index);     
BindingContext[DataTable].EndCurrentEdit(); 

dataAdapter.Update(DataTable);    
DataTable.AcceptChanges();
生活了然无味 2024-10-12 03:08:22

对我来说解决这个问题的方法是调用 DataRow 上的 Delete 方法,而不是调用 DataTable 上的 Remove 方法。

ds.Tables["localitati"].Rows.Find(primaryKeyValue).Delete();

或者只是简单地

dr.Delete();

What fixed it for me was to call the Delete method on the DataRow instead of the Remove method on the DataTable.

ds.Tables["localitati"].Rows.Find(primaryKeyValue).Delete();

or just simply

dr.Delete();
软的没边 2024-10-12 03:08:22

您需要确保已设置 da1.DeleteCommand - 这是将为 DataTable 中已删除的每一行触发的命令。例如,请参阅此 MSDN 参考

You need to make sure you've set the da1.DeleteCommand - this is the command that will be fired for each row in the DataTable that has been deleted. See this MSDN reference for example.

带刺的爱情 2024-10-12 03:08:22

尝试下面的代码
假设数据库没有抛出任何异常。

con.Open() 
da1.Update(ds1.Tables["localitati"].GetChanges()); 
con.Close(); 

try below code
assuming that Database is not throwing any exception.

con.Open() 
da1.Update(ds1.Tables["localitati"].GetChanges()); 
con.Close(); 
悲欢浪云 2024-10-12 03:08:22

在您发布的代码中,仅为 DataAdapter 设置了 SelectCommand。
您可以使用以下代码为 da1 生成插入、更新和删除命令。

string sql = "SELECT * From localitati";
da1 = new SqlDataAdapter(sql, con);
SqlCommandBuilder builder = new SqlCommandBuilder(da1);
builder.QuotePrefix = "[";
builder.QuoteSuffix = "]";
da1.Fill(ds1, "localitati");

然而,CommandBuilder 只能用于相对简单的场景(详细信息)。至于其余的,建议编写自己的依赖于自定义命令文本/存储过程的命令。

如果仍然不起作用,您可以在服务器上启动 SQL Server Profiler 来跟踪执行 Update 方法时哪些命令访问了数据库。

In the code that you have posted only the SelectCommand is set for the DataAdapter.
You could use the following code to generate the Insert, Update and Delete commands for da1.

string sql = "SELECT * From localitati";
da1 = new SqlDataAdapter(sql, con);
SqlCommandBuilder builder = new SqlCommandBuilder(da1);
builder.QuotePrefix = "[";
builder.QuoteSuffix = "]";
da1.Fill(ds1, "localitati");

The CommandBuilder however should be used only for relatively simple scenarios (details). For he rest is recommended to write your own commands that rely on custom command texts/stored procedures.

If it still doesn't work, you could start a SQL Server Profiler on the server to trace what command gets to the database when you execute the Update method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文