我试图使用SQLDataAdapter
从表中删除数据,并且为此我需要给它一个DeleteCommand
。创建SQLCommand以便在SQLDataAdapter中使用
的SQL我会用删除行是:
DELETE FROM table WHERE ID = x
的问题是这样的:我如何指定的DataAdapter
做什么用替代x
?生成DataAdapter
的SQL与其被告知要更新的数据表(外连接)略有不同(无连接)。
我该怎么做?
我试图使用SQLDataAdapter
从表中删除数据,并且为此我需要给它一个DeleteCommand
。创建SQLCommand以便在SQLDataAdapter中使用
的SQL我会用删除行是:
DELETE FROM table WHERE ID = x
的问题是这样的:我如何指定的DataAdapter
做什么用替代x
?生成DataAdapter
的SQL与其被告知要更新的数据表(外连接)略有不同(无连接)。
我该怎么做?
在这里,您可以传递参数删除命令:
// Create the DeleteCommand.
command = new SqlCommand(
"DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);
// Add the parameters for the DeleteCommand.
parameter = command.Parameters.Add(
"@CustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;
adapter.DeleteCommand = command;
从MSDN
使用SourceColumn和SourceVersion的SqlParameter属性:
var deleteCommand = connection.CreateCommand();
deleteCommand = "DELETE FROM table WHERE ID = @ID";
var param = new SqlParameter("ID");
param.SourceColumn = "the Select Column";
param.SourceVersion = DataRowVersion.Original;
deleteCommand.Parameters.Add (param);
采取的代码,你能解释一下其中的SourceVersion是什么? – Malfist 2009-03-04 20:11:56
SourceVersion指示将使用哪个值,Original是您使用select查询获得的原始值,Current是该字段的当前值,就像您修改它一样。 – albertein 2009-03-04 20:13:39