手动设置SelectCommand、InsertCommand、UpdateCommand 和 DeleteCommand 属性
2011-10-24 15:50
363 查看
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 SelectCommand,InsertCommand,UpdateCommand,DeleteCommand 属性
- MYSQL数据库(十)- 数据表的插入(insert)、删(delete)、改(update)、查(select)、group by 分组、having语句设置分组条件,order by查询结果排序,
- MyBatis的常见属性总结select、insert、update、delete
- 我想操作的是利用SqlDataAdapter的几个Command属性(InsertCommand,UpdateCommand,DeleteCommand)来更新数据库
- mybatis中----select、insert、update、delete主要操作方法与属性
- ASP.NET2.0设置sqlDataSource的Select、Insert、Update与Delete命令[转]
- SqlDataAdapter有关InsertCommand,UpdateCommand,DeleteCommand 实例
- ASP.NET2.0设置sqlDataSource的Select、Insert、Update与Delete命令
- SqlDataAdapter有关InsertCommand,UpdateCommand,DeleteCommand 实例
- 在MyBatis的select、insert、update、delete这些元素中都提到了parameterType这个属性。MyBatis现在可以使用的parameterType有基本数据类型和Ja
- SQL的insertcommand、update、deletecommand的使用
- MyBatis的常见属性总结select、insert、update、delete
- ASP.NET2.0设置sqlDataSource的Select、Insert、Update与Delete命令
- mybatis——select、insert、update、delete
- mybatis select、insert、update、delete标签
- SQL_4th_插入insert、查询select、删除delete、更新update
- 根据表名自动生成INSERT,UPDATE,DELETE,SELECT的SQL语句
- 簡化SQL Insert、Update、Delete、Select的方法
- 扩展GridView控件(3) - 根据按钮的CommandName设置其客户端属性
- 关于strutst2 select标签的disable属性的设置和移除