OdbcDataAdapter.DeleteCommand 属性
获取或设置 SQL 语句或存储过程,用于删除数据源中的记录。
**命名空间:**System.Data.Odbc
**程序集:**System.Data(在 system.data.dll 中)
语法
声明
Public Property DeleteCommand As OdbcCommand
用法
Dim instance As OdbcDataAdapter
Dim value As OdbcCommand
value = instance.DeleteCommand
instance.DeleteCommand = value
public OdbcCommand DeleteCommand { get; set; }
public:
property OdbcCommand^ DeleteCommand {
OdbcCommand^ get ();
void set (OdbcCommand^ value);
}
/** @property */
public OdbcCommand get_DeleteCommand ()
/** @property */
public void set_DeleteCommand (OdbcCommand value)
public function get DeleteCommand () : OdbcCommand
public function set DeleteCommand (value : OdbcCommand)
属性值
在更新操作期间使用的 OdbcCommand,它用来删除数据源中与 DataSet 中已删除的行相对应的记录。
备注
向先前创建的 OdbcCommand 分配 DeleteCommand 属性时,将不会克隆 OdbcCommand。相反,DeleteCommand 维护对以前创建的 OdbcCommand 的引用。
更新操作期间,如果没有设置 DeleteCommand 并且在 DataSet 中存在主键信息,可以使用 OdbcCommandBuilder 类来自动生成 DeleteCommand 和协调 DataSet 与数据源所需的附加命令。要实现这一点,请设置 OdbcDataAdapter 的 SelectCommand 属性。此生成逻辑还要求 DataSet 中存在键列信息。有关更多信息,请参见 自动生成命令。
示例
下面的示例创建一个 OdbcDataAdapter 并设置 SelectCommand 和 DeleteCommand 属性。假定已经创建了一个 OdbcConnection 对象。
Public Function CreateDataAdapter( _
ByVal connection As OdbcConnection) As OdbcDataAdapter
Dim selectCommand As String = _
"SELECT CustomerID, CompanyName FROM Customers"
Dim adapter As OdbcDataAdapter = _
New OdbcDataAdapter(selectCommand, connection)
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
' Create the Insert, Update and Delete commands.
adapter.InsertCommand = New OdbcCommand( _
"INSERT INTO Customers (CustomerID, CompanyName) " & _
"VALUES (?, ?)")
adapter.UpdateCommand = New OdbcCommand( _
"UPDATE Customers SET CustomerID = ?, CompanyName = ? " & _
"WHERE CustomerID = ?")
adapter.DeleteCommand = New OdbcCommand( _
"DELETE FROM Customers WHERE CustomerID = ?")
' Create the parameters.
adapter.InsertCommand.Parameters.Add( _
"@CustomerID", OdbcType.Char, 5, "CustomerID")
adapter.InsertCommand.Parameters.Add( _
"@CompanyName", OdbcType.VarChar, 40, "CompanyName")
adapter.UpdateCommand.Parameters.Add( _
"@CustomerID", OdbcType.Char, 5, "CustomerID")
adapter.UpdateCommand.Parameters.Add( _
"@CompanyName", OdbcType.VarChar, 40, "CompanyName")
adapter.UpdateCommand.Parameters.Add( _
"@oldCustomerID", OdbcType.Char, 5, "CustomerID").SourceVersion = _
DataRowVersion.Original
adapter.DeleteCommand.Parameters.Add( _
"@CustomerID", OdbcType.Char, 5, "CustomerID").SourceVersion = _
DataRowVersion.Original
Return adapter
End Function
public static OdbcDataAdapter CreateDataAdapter(
OdbcConnection connection)
{
string selectCommand =
"SELECT CustomerID, CompanyName FROM Customers";
OdbcDataAdapter adapter = new OdbcDataAdapter(
selectCommand, connection);
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
// Create the Insert, Update and Delete commands.
adapter.InsertCommand = new OdbcCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (?, ?)");
adapter.UpdateCommand = new OdbcCommand(
"UPDATE Customers SET CustomerID = ?, CompanyName = ? " +
"WHERE CustomerID = ?");
adapter.DeleteCommand = new OdbcCommand(
"DELETE FROM Customers WHERE CustomerID = ?");
// Create the parameters.
adapter.InsertCommand.Parameters.Add("@CustomerID",
OdbcType.Char, 5, "CustomerID");
adapter.InsertCommand.Parameters.Add("@CompanyName",
OdbcType.VarChar, 40, "CompanyName");
adapter.UpdateCommand.Parameters.Add("@CustomerID",
OdbcType.Char, 5, "CustomerID");
adapter.UpdateCommand.Parameters.Add("@CompanyName",
OdbcType.VarChar, 40, "CompanyName");
adapter.UpdateCommand.Parameters.Add("@oldCustomerID",
OdbcType.Char, 5, "CustomerID").SourceVersion =
DataRowVersion.Original;
adapter.DeleteCommand.Parameters.Add("@CustomerID",
OdbcType.Char, 5, "CustomerID").SourceVersion =
DataRowVersion.Original;
return adapter;
}
平台
Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1
请参见
参考
OdbcDataAdapter 类
OdbcDataAdapter 成员
System.Data.Odbc 命名空间