OdbcDataAdapter.SelectCommand 属性

获取或设置 SQL 语句或存储过程,用于选择数据源中的记录。

**命名空间:**System.Data.Odbc
**程序集:**System.Data(在 system.data.dll 中)

语法

声明
Public Property SelectCommand As OdbcCommand
用法
Dim instance As OdbcDataAdapter
Dim value As OdbcCommand

value = instance.SelectCommand

instance.SelectCommand = value
public OdbcCommand SelectCommand { get; set; }
public:
property OdbcCommand^ SelectCommand {
    OdbcCommand^ get ();
    void set (OdbcCommand^ value);
}
/** @property */
public OdbcCommand get_SelectCommand ()

/** @property */
public void set_SelectCommand (OdbcCommand value)
public function get SelectCommand () : OdbcCommand

public function set SelectCommand (value : OdbcCommand)

属性值

OdbcCommand,在填充操作过程中用于从数据源选择记录以放置在 DataSet 中。

备注

在将 SelectCommand 分配给以前创建的 OdbcCommand 时,不克隆 OdbcCommand。相反,SelectCommand 维护对以前创建的 OdbcCommand 对象的引用。

如果 SelectCommand 不返回任何行,则不会向 DataSet 添加任何表,并且不会引发任何异常。

示例

下面的示例创建一个 OdbcDataAdapter 并设置 SelectCommandInsertCommand 属性。假定已经创建了一个 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 命名空间

其他资源

使用 DataAdapter