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)
Insert into tblStudents values('James Bynes','Male',720)
Insert into tblStudents values('Mary Ward','Female',870)
Insert into tblStudents values('Nick Niron','Male',680)
2.WebForm代码
<div style="font-family: Arial">
<table border="1">
<tr>
<td>
Student ID
td>
<td>
<asp:TextBox ID="txtStudentID" runat="server">asp:TextBox>
<asp:Button ID="btnGetStudent" runat="server" Text="Load"
OnClick="btnGetStudent_Click" />
td>
tr>
<tr>
<td>
Name
td>
<td>
<asp:TextBox ID="txtStudentName" runat="server">asp:TextBox>
td>
tr>
<tr>
<td>
Gender
td>
<td>
<asp:DropDownList ID="ddlGender" runat="server">
<asp:ListItem Text="Select Gender" Value="-1">asp:ListItem>
<asp:ListItem Text="Male" Value="Male">asp:ListItem>
<asp:ListItem Text="Female" Value="Female">asp:ListItem>
asp:DropDownList>
td>
tr>
<tr>
<td>
Total Marks
td>
<td>
<asp:TextBox ID="txtTotalMarks" runat="server">asp:TextBox>
td>
tr>
<tr>
<td colspan="2">
<asp:Button ID="btnUpdate" runat="server" Text="Update"
OnClick="btnUpdate_Click" />
<asp:Label ID="lblStatus" runat="server" Font-Bold="true">
asp:Label>
td>
tr>
table>
div>
Load
按钮根据ID查询,Update
按钮更新数据
查询事件:
protected void btnGetStudent_Click(object sender, EventArgs e)
{
string CS = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(CS);
string sqlQuery = "select * from tblStudents where ID = " + txtStudentID.Text;
SqlDataAdapter da = new SqlDataAdapter(sqlQuery, con);
DataSet ds = new DataSet();
da.Fill(ds, "Students");
//在ViewState中保存DataSet和查询语句,将来用来SqlCommandBuilder生成命令
ViewState["SQL_QUERY"] = sqlQuery;
ViewState["DATASET"] = ds;
if (ds.Tables["Students"].Rows.Count > 0)
{
DataRow dr = ds.Tables["Students"].Rows[0];
txtStudentName.Text = dr["Name"].ToString();
txtTotalMarks.Text = dr["TotalMarks"].ToString();
ddlGender.SelectedValue = dr["Gender"].ToString();
}
else
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "No Student record with ID = " + txtStudentID.Text;
}
}
更新事件:
protected void btnUpdate_Click(object sender, EventArgs e)
{
string CS = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(CS);
SqlDataAdapter da = new SqlDataAdapter((string)ViewState["SQL_QUERY"], con);
SqlCommandBuilder builder = new SqlCommandBuilder(da);
DataSet ds = (DataSet)ViewState["DATASET"];
if (ds.Tables["Students"].Rows.Count > 0)
{
DataRow dr = ds.Tables["Students"].Rows[0];
dr["Name"] = txtStudentName.Text;
dr["Gender"] = ddlGender.SelectedValue;
dr["TotalMarks"] = txtTotalMarks.Text;
dr["Id"] = txtStudentID.Text;
}
int rowsUpdated = da.Update(ds, "Students");
if (rowsUpdated == 0)
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "No rows updated";
}
else
{
lblStatus.ForeColor = System.Drawing.Color.Green;
lblStatus.Text = rowsUpdated.ToString() + " row(s) updated";
}
}
SqlDataAdapter.Update
无效的两个原因:
1.SqlDataAdapter
对象没有绑定SqlCommandBuilder
对象
2.SqlDataAdapter
的SelectCommand
,没有返回至少一个主键或者唯一的列
如果想查看自动生成的 INSERT
, UPDATE
, 和DELETE
语句,使用GetInsertCommand()
,GetUpdateCommand()
和GetDeleteCommand()
lblInsert.Text = builder.GetInsertCommand().CommandText;
lblUpdate.Text = builder.GetUpdateCommand().CommandText;
lblDelete.Text = builder.GetDeleteCommand().CommandText;