SqlDataAdapter SelectCommand,InsertCommand,UpdateCommand,DeleteCommand 属性
2010-02-08 00:01
519 查看
public static SqlDataAdapter CreateCustomerAdapter( SqlConnection connection) { SqlDataAdapter adapter = new SqlDataAdapter(); // Create the SelectCommand. SqlCommand command = new SqlCommand("SELECT * FROM Customers " + "WHERE Country = @Country AND City = @City", connection); // Add the parameters for the SelectCommand. command.Parameters.Add("@Country", SqlDbType.NVarChar, 15); command.Parameters.Add("@City", SqlDbType.NVarChar, 15); adapter.SelectCommand = command; // Create the InsertCommand. command = new SqlCommand( "INSERT INTO Customers (CustomerID, CompanyName) " + "VALUES (@CustomerID, @CompanyName)", connection); // Add the parameters for the InsertCommand. command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID"); command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName"); adapter.InsertCommand = command; // Create the UpdateCommand. command = new SqlCommand( "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " + "WHERE CustomerID = @oldCustomerID", connection); // Add the parameters for the UpdateCommand. command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID"); command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName"); SqlParameter parameter = command.Parameters.Add( "@oldCustomerID", SqlDbType.NChar, 5, "CustomerID"); parameter.SourceVersion = DataRowVersion.Original; adapter.UpdateCommand = command; // Create the DeleteCommand. command = new SqlCommand( "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection); // Add the parameters for the DeleteCommand. parameter = command.Parameters.Add( "@CustomerID", SqlDbType.NChar, 5, "CustomerID"); parameter.SourceVersion = DataRowVersion.Original; adapter.DeleteCommand = command; return adapter; }
相关文章推荐
- 我想操作的是利用SqlDataAdapter的几个Command属性(InsertCommand,UpdateCommand,DeleteCommand)来更新数据库
- SqlDataAdapter有关InsertCommand,UpdateCommand,DeleteCommand 实例
- SqlDataAdapter有关InsertCommand,UpdateCommand,DeleteCommand 实例
- SqlDataAdapter.UpdateCommand 属性(极易出错的地方)
- SqlDataAdapter.UpdateCommand 属性(极易出错的地方)(转自:http://blog.csdn.net/tsapi/archive/2011/03/29/6286654.aspx)
- 构造SqlDataAdapter InsertCommand和UpdateCommand 帮助类解决并发冲突
- Use SqlDataAdapter to update database, but SqlCommandBuilder required
- SqlDataAdapter SqlCommandBuilder update 批量更新
- SqlDataAdapter.Update()方法与SqlCommandBuilder
- SqlDataAdapter.Update()方法与SqlCommandBuilder(转)
- 对于不返回任何键列信息的 SelectCommand,不支持 UpdateCommand、DeleteCommand 的动态 SQL 生成
- 用SqlDataAdapter.DeleteCommand根据DataSet删除数据库记录的失败教训
- SqlDataAdapter.Update()方法与SqlCommandBuilder
- DataTable数据批量插入数据的库三种方法:SqlCommand.EcecuteNonQurery(),SqlDataAdapter.Update(DataTable) ,SqlBulkCopy.
- 手动设置SelectCommand、InsertCommand、UpdateCommand 和 DeleteCommand 属性
- SqlDataAdapter.Update()方法与SqlCommandBuilder
- SQL的insertcommand、update、deletecommand的使用
- SqlDataAdapter的InsertCommand属性
- datable合并sqldataadapter不能update的思考
- 对于不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成”问题的解决