-
SqlCommandBuilder 用来为 SqlDataAdapter 自动产生SQL语句 ,批量更新DataSet, 不用再为SqlDataAdapter 写好 InsertCommand,DeleteCommand,UpdateCommand等Sql语句.如果对DataSet(注意是内存里)的修改(比如,遍历ds里的Tittle表修改某些字段,或者绑定到datagridview修改)可以通过Update自动更新到数据库。当你不为SqlDataAdapter 指定SqlCommandBuilder的时候,你就必须手动为 SqlDataAdapter 写好 InsertCommand,DeleteCommand,UpdateCommand语句,否则是无法调用Update方法来更新的。
-
-
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Linq;
-
using System.Text;
-
using System.Windows.Forms;
-
using System.Data.SqlClient;
-
-
namespace WindowsFormsApplication1
-
{
-
public partial class Form1 : Form
-
{
-
SqlConnection conn;
-
DataSet ds;
-
SqlDataAdapter sda;
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
-
private void button1_Click(object sender, EventArgs e)
-
{
-
DataTable dt = new DataTable(); //创建一个表
-
dt = ds.Tables[ 0]; //将DataSet中的数据赋值给dt表
-
sda.FillSchema(dt, SchemaType.Mapped); //FillSchema是用来向DataTable中填入详细的元数据信息的,例如(column names, primary key, constraints等),但不填入数据。
-
DataRow dr = dt.Rows.Find(txtID.Text); //表示DataTable中的一行数据
-
//设置DataRow中的值
-
dr[ "orderdate"] = txtorderdate.Text;
-
dr[ "ordercode"] = txtordercode.Text;
-
dr[ "materialscode"] = txtmaterialscode.Text;
-
dr[ "materialsquantity"] = txtmaterialsquantity.Text;
-
-
//使用SqlCommandBuilder的好处在于你对DataSet(注意是内存里)的修改(比如,你遍历ds里的Tittle表修改某些字段,
-
//或者绑定到datagridview修改)可以通过Update自动更新到数据库
-
SqlCommandBuilder cmdbuilder = new SqlCommandBuilder(sda);
-
sda.Update(dt);
-
-
}
-
-
private void Form1_Load(object sender, EventArgs e)
-
{
-
conn = new SqlConnection( "server =D008;database =GoogolOrderMangment;uid = sa;pwd = 123456");
-
SqlCommand cmd = new SqlCommand( "select * from materialsdownload ", conn);
-
sda = new SqlDataAdapter();
-
sda.SelectCommand = cmd;
-
ds = new DataSet();
-
sda.Fill(ds, "cs"); //将数据库数据填充到dataset数据表中
-
dataGridView1.DataSource = ds.Tables[ 0];
-
}
-
}
-
}
<p> </p> </li>
-
//使用SqlCommandBuilder的好处在于你对DataSet(注意是内存里)的修改(比如,你遍历ds里的Tittle表修改某些字段,
//或者绑定到datagridview修改)可以通过Update自动更新到数据库