OdbcDataAdapter.InsertCommand プロパティ
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
データ ソースに新しいレコードを挿入するために使用する SQL ステートメントまたはストアド プロシージャを取得または設定します。
public:
property System::Data::Odbc::OdbcCommand ^ InsertCommand { System::Data::Odbc::OdbcCommand ^ get(); void set(System::Data::Odbc::OdbcCommand ^ value); };
public System.Data.Odbc.OdbcCommand InsertCommand { get; set; }
member this.InsertCommand : System.Data.Odbc.OdbcCommand with get, set
Public Property InsertCommand 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
注釈
以前に作成したInsertCommand オブジェクトに OdbcCommand プロパティが割り当てられている場合、OdbcCommandは複製されません。 代わりに、 InsertCommand は、以前に作成した OdbcCommandへの参照を保持します。
更新操作中に、 InsertCommand が設定されておらず、主キー情報が DataSetに存在する場合は、 OdbcCommandBuilder クラスを使用して InsertCommandを自動的に生成し、データ ソースに DataSet を調整するために必要な追加のコマンドを使用できます。 これを行うには、SelectCommandのOdbcDataAdapterプロパティを設定します。 生成ロジックでは、キー列の情報が DataSetに存在する必要もあります。 詳細については、「CommandBuilder でのコマンドの生成」を参照してください。
Note
このコマンドを実行すると行が返される場合は、DataSet オブジェクトのUpdatedRowSource プロパティの設定方法に応じて、これらの行がOdbcCommandに追加される可能性があります。