OdbcDataAdapter 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 OdbcDataAdapter 類別的新執行個體。
多載
OdbcDataAdapter() |
初始化 OdbcDataAdapter 類別的新執行個體。 |
OdbcDataAdapter(OdbcCommand) |
使用指定的 SQL SELECT 陳述式來初始化 OdbcDataAdapter 類別的新執行個體。 |
OdbcDataAdapter(String, OdbcConnection) |
使用 SQL SELECT 陳述式和 OdbcDataAdapter 初始化 OdbcConnection 類別的新執行個體。 |
OdbcDataAdapter(String, String) |
使用 SQL SELECT 陳述式和連接字串來初始化 OdbcDataAdapter 類別的新執行個體。 |
OdbcDataAdapter()
初始化 OdbcDataAdapter 類別的新執行個體。
public:
OdbcDataAdapter();
public OdbcDataAdapter ();
Public Sub New ()
範例
下列範例會建立 並 OdbcDataAdapter 設定其部分屬性。
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
備註
當您建立的 OdbcDataAdapter實例時,下列只讀和只讀屬性會設定為其預設值,如表格所示。
屬性 | 預設值 |
---|---|
MissingMappingAction | MissingMappingAction.Passthrough |
MissingSchemaAction | MissingSchemaAction.Add |
您可以透過對 屬性的個別呼叫來變更這些屬性的值。
另請參閱
適用於
OdbcDataAdapter(OdbcCommand)
使用指定的 SQL SELECT 陳述式來初始化 OdbcDataAdapter 類別的新執行個體。
public:
OdbcDataAdapter(System::Data::Odbc::OdbcCommand ^ selectCommand);
public OdbcDataAdapter (System.Data.Odbc.OdbcCommand? selectCommand);
public OdbcDataAdapter (System.Data.Odbc.OdbcCommand selectCommand);
new System.Data.Odbc.OdbcDataAdapter : System.Data.Odbc.OdbcCommand -> System.Data.Odbc.OdbcDataAdapter
Public Sub New (selectCommand As OdbcCommand)
參數
- selectCommand
- OdbcCommand
OdbcCommand,其為 SQL SELECT 陳述式或預存程序,且設定成 SelectCommand 的 OdbcDataAdapter 屬性。
範例
下列範例會建立 並 OdbcDataAdapter 設定其部分屬性。
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
備註
這個建構函式的實作會將 OdbcDataAdapterSelectCommand 屬性設定為 參數中指定的 selectCommand
值。
當您建立的 OdbcDataAdapter實例時,下列只讀和只讀屬性會設定為其預設值,如表格所示。
屬性 | 初始值 |
---|---|
MissingMappingAction | MissingMappingAction.Passthrough |
MissingSchemaAction | MissingSchemaAction.Add |
您可以透過對 屬性的個別呼叫來變更這些屬性的值。
另請參閱
適用於
OdbcDataAdapter(String, OdbcConnection)
使用 SQL SELECT 陳述式和 OdbcDataAdapter 初始化 OdbcConnection 類別的新執行個體。
public:
OdbcDataAdapter(System::String ^ selectCommandText, System::Data::Odbc::OdbcConnection ^ selectConnection);
public OdbcDataAdapter (string? selectCommandText, System.Data.Odbc.OdbcConnection? selectConnection);
public OdbcDataAdapter (string selectCommandText, System.Data.Odbc.OdbcConnection selectConnection);
new System.Data.Odbc.OdbcDataAdapter : string * System.Data.Odbc.OdbcConnection -> System.Data.Odbc.OdbcDataAdapter
Public Sub New (selectCommandText As String, selectConnection As OdbcConnection)
參數
- selectCommandText
- String
字串,將由 SelectCommand 的 OdbcDataAdapter 屬性使用的 SQL SELECT 陳述式或預存程序。
- selectConnection
- OdbcConnection
表示連接的 OdbcConnection。
範例
下列範例會建立 並 OdbcDataAdapter 設定其部分屬性。
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
備註
這個實作OdbcDataAdapter在必須針對兩個或多個OdbcDataAdapter物件呼叫 Fill
方法的應用程式中很有用。
另請參閱
適用於
OdbcDataAdapter(String, String)
使用 SQL SELECT 陳述式和連接字串來初始化 OdbcDataAdapter 類別的新執行個體。
public:
OdbcDataAdapter(System::String ^ selectCommandText, System::String ^ selectConnectionString);
public OdbcDataAdapter (string? selectCommandText, string? selectConnectionString);
public OdbcDataAdapter (string selectCommandText, string selectConnectionString);
new System.Data.Odbc.OdbcDataAdapter : string * string -> System.Data.Odbc.OdbcDataAdapter
Public Sub New (selectCommandText As String, selectConnectionString As String)
參數
- selectCommandText
- String
字串,將由 SelectCommand 的 OdbcDataAdapter 屬性使用的 SQL SELECT 陳述式或預存程序。
- selectConnectionString
- String
連接字串。
範例
下列範例會建立 並 OdbcDataAdapter 設定其部分屬性。
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
備註
建構函式的 OdbcDataAdapter 這個多載會 selectConnectionString
使用 參數來設定 SelectCommand 屬性。 不過,它不會開啟連線。 您仍然必須明確開啟連線。