ADO.NET学习之SqlCommandBuilder
SqlCommandBuilder
SqlCommandBuilder automatically generates INSERT, UPDATE and DELETE sql statements based on the SELECT statement for a single table.
SqlCommandBuilder
会基于单个表的SELECT
语句,自动生成INSERT
、UPDATE
和DELETE
的sql
语句。
使用SqlCommandBuilder
的两个步骤:
1.设置SqlDataAdapter
的SelectCommand
属性
SqlDataAdapter dataAdapter = new SqlDataAdapter();
dataAdapter.SelectCommand = new SqlCommand("SELECT_Query", con);
2.创建SqlCommandBuilder
的一个实例,并制定其DataAdapter
属性的值
SqlCommandBuilder builder = new SqlCommandBuilder();
builder.DataAdapter = dataAdapter;
也可以一步完成,如下:
SqlCommandBuilder builder = new SqlCommandBuilder(dataAdapter);
通过例子说明
准备工作
1.创建表tblStudents
,并添加数据
Create Table tblStudents
(
ID int identity primary key,
Name nvarchar(50),
Gender nvarchar(20),
TotalMarks int
)
Insert into tblStudents values('Mark Hastings','Male',900)
Insert into tblStudents values('Pam Nicholas','Female',760)
Insert into tblStudents values('John Stenson','Male',980)
Insert into tblStudents values('Ram Gerald','Male',990)
Insert into tblStudents values('Ron Simpson','Male',440)
Insert into tblStudents values('Able Wicht','Male',320)
Insert into tblStudents values('Steve Thompson','Male',983)
Inser