在Access数据库中删除行的问题 [英] Problem with deleting row in access database
本文介绍了在Access数据库中删除行的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在尝试删除Access数据库中的一行,但是当我尝试更新数据集时,它给了我这个错误:
"Update requires a valid DeleteCommand when passed DataRow collection with deleted rows.
"
我试图自己解决问题,但似乎无法解决.
如果有人可以给我任何建议,我将非常感激.
这是我的代码.
感谢您提前提供的所有帮助.
Hello guys,
I`m trying to delete a row in an access database, but when I try to update the dataset it give me this error:
"Update requires a valid DeleteCommand when passed DataRow collection with deleted rows.
"
I tried to solve it on my own, but I can''t seem to fix it.
If anyone can give me any advice I will be very thankful.
Here is my code.
Thanks for all the help in advance.
currentRow = e.RowIndex;
ds1 = new DataSet();
con = new System.Data.OleDb.OleDbConnection();
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=DataSource/PhoneBookData.mdb";
con.Open();
string sql = "SELECT * From CONTACT";
da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
da.Fill(ds1, "CONTACT");
DataRow dRow = ds1.Tables["CONTACT"].Rows[0];
ds1.Tables["CONTACT"].Rows[currentRow].Delete();
da.Update(ds1, "CONTACT");
推荐答案
您需要使用SQLCommandBuilder
来创建删除命令.(请记住,使用SQLCommandBuilder始终需要主键)
或者,您可以尝试此 [
You need to useSQLCommandBuilder
for creating the delete command.(Remember you always need primary key for using SQLCommandBuilder)
Or you can try This[^] for SQLDataAdapter without using SQLCommandBuilder.
for further queries comment here!
hope it helps :)
您需要创建一个OledbCommand,然后将其分配给DataAdapter,如下所示:-
You need to create an OledbCommand and then assign it to the DataAdapter, something like this:-
command = new OleDbCommand(
"DELETE * FROM Contacts WHERE ContactID = ?",
con);
parameter = command.Parameters.Add(
"ContactID", OleDbType.Char, 5, "ContactID");
parameter.SourceVersion = DataRowVersion.Original;
dataAdapter.DeleteCommand = command;
希望对您有帮助
Hope this helps
这篇关于在Access数据库中删除行的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文