OdbcDataAdapter.SelectCommand 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定用來在資料來源中選取資料錄的 SQL 陳述式或預存程序。
public:
property System::Data::Odbc::OdbcCommand ^ SelectCommand { System::Data::Odbc::OdbcCommand ^ get(); void set(System::Data::Odbc::OdbcCommand ^ value); };
public System.Data.Odbc.OdbcCommand? SelectCommand { get; set; }
public System.Data.Odbc.OdbcCommand SelectCommand { get; set; }
member this.SelectCommand : System.Data.Odbc.OdbcCommand with get, set
Public Property SelectCommand As OdbcCommand
屬性值
OdbcCommand,用來在填入作業期間為 DataSet 中的位置從資料來源中選取資料錄。
範例
下列範例會 OdbcDataAdapter 建立 並設定 SelectCommand 和 InsertCommand 屬性。 假設您已經建立 OdbcConnection 物件。
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;
}
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
備註
當 指定先前建立OdbcCommand的時SelectCommand,OdbcCommand不會複製 。 相反地,會 SelectCommand 維護先前建立 OdbcCommand 對象的參考。
如果沒有 SelectCommand 傳回任何數據列,則不會將任何數據表加入 DataSet至 ,而且不會引發任何例外狀況。