ADO.NET使用dataadapter从表中删除行 [英] ADO.NET deleting row from a table using dataadapter

查看:163
本文介绍了ADO.NET使用dataadapter从表中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ivé尝试使用dataset.tables.rows.remove()从表中删除行;在remove函数中指定要删除的行.我所做的就是这样.

ivé been trying to delete a row from a table using dataset.tables.rows.remove(); specifying the row to be deleted in the remove function.what i did is exactly like this.

SqlConnection con = new SqlConnection();
con.ConnectionString = "initial catalog=employee;data source=192.168.1.2;user id=sa;password=*********";
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select *from dbo.employeeage", con);
SqlCommandBuilder cmdbuilder = new SqlCommandBuilder(da);
           
DataSet ds = new DataSet("employee");
da.Fill(ds, "employee");
DataRow row = ds.Tables["employee"].Rows[2];
            
ds.Tables["employee"].Rows.Remove(row);
da.Update(ds, "employee");
con.Close();



数据库中什么都没有改变.employeeage表中有30条记录.
我是ado.net的绝对新手,如果我在某个地方输入错误,请纠正我

添加了代码块. [/Edit]



nothing gets changed in the database.i have like 30 records in the employeeage table.
i''m an absolute newbie to ado.net plzz correct me if i am wrong somewhere

Added code block. [/Edit]

推荐答案

代替行
ds.Tables["employee"].Rows.Remove(row);


只需使用


just use

row.Delete();


背后的逻辑是 DataTable 只是一个脱机的内存中对象,表示来自实际数据库表的数据.您将真正的数据库员工表填充到离线


The logic behind is that DataTable is just an off-line in-memory object representing data from your real DB table. You filled real DB employee table into your off-line DataSet ds object by calling

da.Fill(ds, "employee");

>
然后,您通过代码片段引用存储在 ds 数据集中名为"employee"的数据表


Then you reference the filled table, stored in ds DataSet, named "employee", by the code fragment

ds.Tables["employee"]


这将返回类型为DataTable的对象.因此,
DataTable.Rows属性为只是 DataRow.RowState属性 DataRow.RowState属性,并且可以通过 DataRow.Item属性(字符串, DataRowVersion).到目前为止,没有任何东西反映在实际的数据库中.直到您致电


This returns the object of type DataTable. Consequently, DataTable.Rows Property is just an off-line in-memory collection of DataRow objects filled from your real DB. You can manipulate each DataRow object by changing its value. Changes are tracked by DataRow.RowState Property and various versions of field values are accessible through DataRow.Item Property (String, DataRowVersion). Nothing reflects in real DB so far. Until you call

da.Update(ds, "employee");


执行 DataAdapter.Update方法 ,数据适配器将遍历您的离线DataTable.Rows员工"表的集合.对于每个 row (DataRow类型), row.RowState


Performing DataAdapter.Update Method, data adapter walks through your off-line DataTable.Rows collection of "employee" table. For each row (of DataRow type), row.RowState is one of DataRowState Enum. If adapter finds row.RowState = DataRowState.Added then it tries to call Insert command on real DB, filling in values from this off-line row("current" version of each corresponding field). If row.RowState = DataRowState.Modified, data adapter tries to call Update command on real DB, matching the row.PrimaryKey with the real primary key of the real DB table. If row.RowState = DataRowState.Deleted, the data adapter tries to call Delete command on real DB, matching the row.PrimaryKey with the real primary key of the real DB table. If row.RowState = DataRowState.Unchanged or the row was removed from the Rows collection, then data adapter does nothing in real DB for matching (or missing) row..

So this is it. As you completely removed your off-line representation of your real DB row by calling

ds.Tables["employee"].Rows.Remove(row);


则数据适配器 da 不再具有对该实际数据库行的引用.
另一方面,如果找到离线DataRow row 对象并调用


then the data adapter da has no reference to that real DB row anymore.
On the other side, if you find your off-line DataRow row object and call

row.Delete();


然后将 row.RowState 设置为 DataRowState.Deleted ,随后将


then the row.RowState is set to DataRowState.Deleted and the consequent

da.Update(ds, "employee");


查找标记为已删除,并在实际数据库上调用Delete命令.

希望这会有所帮助,
madvad


finds the row marked Deleted and calls the Delete command on real DB.

Hope this helps,
madvad


您需要为适配器设置DeleteCommand属性.
You need to set the DeleteCommand property for the adapter.


如丹麦语所示,您可以使用SQLDataAdapter中的DeleteCommand
查看此链接可能会帮助您...

DeleteCommand [
As Danish suggest you can use DeleteCommand of SQLDataAdapter
review this link it might helps you...

DeleteCommand[^]


这篇关于ADO.NET使用dataadapter从表中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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