SqlCommandBuilder的作用

最近看一个案例,关于如何向数据库传数据,颇有发现。

以前我的数据都是采用Sql代码用SqlCommand一条一条语句的传入数据库。

但是今天发现有更好的方法。

代码如下:

SqlConnection cn=new SqlConnection(ConStr);

   DataSet DS=new DataSet();

   SqlDataAdapter Sda=new SqlDataAdapter("Select * from Table1",cn);

   try

   {

    Sda.Fill(DS,"Table1");

    MessageBox.Show(DS.Tables[0].Rows.Count.ToString());

   }

   catch(Exception er)

   {

    MessageBox.Show(er.Message);

   }

   //DataSet DS2=DS.Clone();

   //Sda.SelectCommand=new SqlCommand("Select * from Table1");

   DataRow dr=DS.Tables[0].NewRow();

   dr["F1"]=5;

   dr["F2"]=5;

   dr["F3"]=5;

   DS.Tables[0].Rows.Add(dr);

   dr=DS.Tables[0].NewRow();

   dr["F1"]=6;

   dr["F2"]=6;

   dr["F3"]=6;

   DS.Tables[0].Rows.Add(dr);

   

   SqlCommandBuilder SCB=new SqlCommandBuilder(Sda);

   

   try

   {

    Sda.Update(DS,"Table1");

   }

   catch(Exception er)

   {

    MessageBox.Show(er.Message);

   }

  

   

   Sda.Dispose();

   cn.Close();

数据库用的是SQL2000,先在数据库中建立Table1的临时表,建立字段MainID,F1,F2,F3

初始数据如下

MainID     F1    F2    F3

1                1        1    1

2                2        2    2

3                3        3    3

4                4        4    4

注意到,我先把数据从数据库中读出来,赋值到DS中

结果显然为4行

然后我在该数据集DS中添加2个同构行

这是注意了,我没有做任何的Insert相关语句

打算直接采用SqlDataAdapter.Update(DataSet,TableScr)方法

但是注意,直接用是不行的,会提示缺少Insert命令的关联

这是可以用了如黄色高亮标记的语句

SqlCommandBuilder SCB=new SqlCommandBuilder(Sda);

这是所有关联就都建立好了,全自动。奇妙,诡异阿

这样就方便多了。

恩,更多的相关功能正在研究中。

 =============================================================

使用SqlCommandBuilder与SqlDataAdapter结合使用,可以方便地去数据库进行更新。只要指定Select 语句就可以自动生成Insert,update,delete语句,但要注意一点。Select 语句中返回的列要包括主键列,否则将无法产生Update,和Delete语句。相应的操作将无法执行

public static DataSet SelectSqlSrvRows(string myConnection, string mySelectQuery, string myTableName)

{

   SqlConnection myConn = new SqlConnection(myConnection);

   SqlDataAdapter myDataAdapter = new SqlDataAdapter();

   myDataAdapter.SelectCommand = new SqlCommand(mySelectQuery, myConn);

   SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter);

   myConn.Open();

   DataSet ds = new DataSet();

   myDataAdapter.Fill(ds, myTableName);

   //code to modify data in DataSet here

   //Without the SqlCommandBuilder this line would fail

   myDataAdapter.Update(ds, myTableName);

   myConn.Close();

   return ds;

}

using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp3 { public partial class Form4 : Form { string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString; SqlConnection connection = null; SqlDataAdapter adapter = null; DataSet ds = null; public Form4() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { connection= new SqlConnection(conStr); adapter=new SqlDataAdapter("select * from studentscores",connection); ds= new DataSet(); adapter.Fill(ds, "studentscores"); dataGridView1.DataSource = ds.Tables["studentscores"]; } private void button2_Click(object sender, EventArgs e) { ds = new DataSet(); SqlCommandBuilder builder = new SqlCommandBuilder(adapter); DataRow r1 = ds.Tables["studentscores"].NewRow(); r1[0]=textBox1.Text; r1[1]=textBox2.Text; r1[2]=textBox3.Text; ds.Tables[0].Rows.Add(r1); adapter.Update(ds,"studentscores"); dataGridView1.DataSource = ds.Tables["studentscores"]; } private void button3_Click(object sender, EventArgs e) { ds = new DataSet(); SqlCommandBuilder builder=new SqlCommandBuilder(adapter); DataRowCollection rows = ds.Tables["studentscores"].Rows; DataRow row; for (int i = 0;i<rows.Count; i++) { row= rows[i]; if (row["num"].ToString() == textBox1.Text) { row["scores"]=textBox3.Text ; } } adapter.Update(ds,"studentscores"); dataGridView1.DataSource = ds.Tables["studentscores"]; } private void button4_Click(object sender, EventArgs e) { SqlCommandBuilder builder=new SqlCommandBuilder(adapter); DataRowCollection rows = ds.Tables["studentscores"].Rows; DataRow row; for(int i = 0; i<rows.Count; i++) { row= rows[i]; if (row["num"].ToString()==textBox1.Text) { row.Delete(); } } adapter.Update(ds,"studentscores"); dataGridView1.DataSource = ds.Tables["studentscores"]; } } }
最新发布
05-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值