System.Data.SqlClient Namespace SqlDataAdapter Class
Sets or retrieves a command to insert new records into the data source.
[ VB ]
Public Property InsertCommand As SqlCommand
[ C# ]
public SqlCommand InsertCommand {get; set;}
[ C++ ]
public: __property SqlCommand* get_InsertCommand ( );
public: __property void set_InsertCommand ( SqlCommand* );
[ JScript ]
public function get InsertCommand ( ) : SqlCommand;
public function set InsertCommand ( SqlCommand );
An SqlCommand used during Update to insert records into the data source that correspond to new rows in the DataSet.
During Update, if this property is not set and primary key information is present in the DataSet, the InsertCommand can be generated automatically if you set the SelectCommand property and use the SqlCommandBuilder. Then, any additional commands that you do not set are generated by the SqlCommandBuilder. This generation logic requires key column information to be present in the DataSet. For more information see Automatically Generated Commands.
When InsertCommand is assigned to a previously created SqlCommand, the SqlCommand is not cloned. The InsertCommand maintains a reference to the previously created SqlCommand object.
NOTE: If execution of this command returns rows, these rows may be added to the DataSet depending on how you set the UpdatedRowSource property of the SqlCommand object.
The following example illustrates use of the InsertCommand property.
void InsertCommandDemo ( ) {
string query = "SELECT * FROM Categories ";
string connString = "uid=sa; pwd=; database=northwind; server=myServer";
SqlDataAdapter myAdapter = new SqlDataAdapter ( query, connString );
myAdapter.InsertCommand.CommandText =
"INSERT INTO Categories ( Category, Description, Picture ) " +
"Values ( @Category, @Description, @Picture ) ";
myAdapter.InsertCommand.Connection =
( SqlConnection ) myAdapter.SelectCommand.Connection.Clone ( );
}
public sub InsertCommandDemo ( )
dim query as string = "SELECT * FROM Categories "
dim connString as string = "uid=sa; pwd=; database=northwind; server=myServer"
dim myAdapter as new SqlDataAdapter ( query, connString )
myAdapter.InsertCommand.CommandText =
"INSERT INTO Categories ( Category, Description, Picture ) " &_
"Values ( @Category, @Description, @Picture ) "
myAdapter.InsertCommand.Connection = CType ( _
myAdapter.SelectCommand.Connection.Clone ( ), SqlConnection )
end sub |
|
C# |
VB |
SqlDataAdapter Members DeleteCommand SelectCommand UpdateCommand