有时候需要手工的定义一个DataAdapter的UpdateCommend、InsertCommand和DeleteCommand,比如将数据库中的一个View绑定到了DataGridView的DataSource,但需要Update、Insert或Delete View中某个表的记录的情况。以下是一个OledbDataAdapter的例子:
InBlock.gifpublic static OleDbDataAdapter CreateDataAdapter(string selectCommand,
InBlock.gif        OleDbConnection connection)
InBlock.gif{
InBlock.gif        OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand, connection);
InBlock.gif
        adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
InBlock.gif
        // Create the Insert, Update and Delete commands.
InBlock.gif        adapter.InsertCommand = new OleDbCommand(
InBlock.gif                "INSERT INTO Customers (CustomerID, CompanyName) " +
InBlock.gif                "VALUES (?, ?)");
InBlock.gif
        adapter.UpdateCommand = new OleDbCommand(
InBlock.gif                "UPDATE Customers SET CustomerID = ?, CompanyName = ? " +
InBlock.gif                "WHERE CustomerID = ?");
InBlock.gif
        adapter.DeleteCommand = new OleDbCommand(
InBlock.gif                "DELETE FROM Customers WHERE CustomerID = ?");
InBlock.gif
        // Create the parameters.
InBlock.gif        adapter.InsertCommand.Parameters.Add("@CustomerID",
InBlock.gif                OleDbType.Char, 5, "CustomerID");
InBlock.gif        adapter.InsertCommand.Parameters.Add("@CompanyName",
InBlock.gif                OleDbType.VarChar, 40, "CompanyName");
InBlock.gif
        adapter.UpdateCommand.Parameters.Add("@CustomerID",
InBlock.gif                OleDbType.Char, 5, "CustomerID");
InBlock.gif        adapter.UpdateCommand.Parameters.Add("@CompanyName",
InBlock.gif                OleDbType.VarChar, 40, "CompanyName");
InBlock.gif        adapter.UpdateCommand.Parameters.Add("@oldCustomerID",
InBlock.gif                OleDbType.Char, 5, "CustomerID").SourceVersion =
InBlock.gif                DataRowVersion.Original;
InBlock.gif
        adapter.DeleteCommand.Parameters.Add("@CustomerID",
InBlock.gif                OleDbType.Char, 5, "CustomerID").SourceVersion =
InBlock.gif                DataRowVersion.Original;
InBlock.gif
        return adapter;
InBlock.gif}
这里用到的OleDbCommand.Parameters.Add()的定义为:
     参数名称、数据类型、列长和源列名称
InBlock.gifAdd(String, OleDbType, Int32, String)