2009-03-04 74 views
1

我试图使用SQLDataAdapter从表中删除数据,并且为此我需要给它一个DeleteCommand创建SQLCommand以便在SQLDataAdapter中使用

的SQL我会用删除行是:

DELETE FROM table WHERE ID = x 

的问题是这样的:我如何指定的DataAdapter做什么用替代x?生成DataAdapter的SQL与其被告知要更新的数据表(外连接)略有不同(无连接)。

我该怎么做?

回答

4

在这里,您可以传递参数删除命令:

// 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

1

使用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); 
+0

采取的代码,你能解释一下其中的SourceVersion是什么? – Malfist 2009-03-04 20:11:56

+0

SourceVersion指示将使用哪个值,Original是您使用select查询获得的原始值,Current是该字段的当前值,就像您修改它一样。 – albertein 2009-03-04 20:13:39

相关问题