SQL整体更新的方法


  本文标签:SQL整体更新

  如果在项目中遇到用户在客户端编辑数据,添加数据.SQL整体更新到数据库里面,应该怎么做呢?下面就将教您如何进行SQL整体更新,供您参考  。
数据库结构:

  1. DataSet ds=new DataSet();读取数据  
  2.     private void button1_Click(object sender, System.EventArgs e)  
  3.         {  
  4.             //  
  5.             string str_Conn="workstation id=JHTCHINA;packet size=4096;user id=sa;initial catalog=master;persist security info=False";  
  6.             try  
  7.             {  
  8.                 SqlConnection objConn=new SqlConnection(str_Conn);  
  9.                 string str_sql="select * from Test_Base";  
  10.                 SqlCommand objComm=new SqlCommand(str_sql,objConn);  
  11.                 SqlDataAdapter objAdapter=new SqlDataAdapter(objComm);                 
  12.                 objAdapter.Fill(ds,"Test_Base");  
  13.                 dataGrid1.DataSource=ds;  
  14.             }  
  15.             catch (Exception exc)  
  16.             {  
  17.                 MessageBox.Show(exc.Message);  
  18.             }  
  19.         } 

  CREATE TABLE [Test_Base] (
    [CodeZZB] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
    [InterName] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
    [Guid] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL CONSTRAINT [DF_LoginPerson_Guid] DEFAULT (newid()),
    CONSTRAINT [PK_Test_Base] PRIMARY KEY  CLUSTERED
    (
        [CodeZZB]
    )  ON [PRIMARY]
) ON [PRIMARY]
GO

  定义 全局DataSet

  1. DataSet ds=new DataSet();读取数据  
  2.     private void button1_Click(object sender, System.EventArgs e)  
  3.         {  
  4.             //  
  5.             string str_Conn="workstation id=JHTCHINA;packet size=4096;user id=sa;initial catalog=master;persist security info=False";  
  6.             try  
  7.             {  
  8.                 SqlConnection objConn=new SqlConnection(str_Conn);  
  9.                 string str_sql="select * from Test_Base";  
  10.                 SqlCommand objComm=new SqlCommand(str_sql,objConn);  
  11.                 SqlDataAdapter objAdapter=new SqlDataAdapter(objComm);                 
  12.                 objAdapter.Fill(ds,"Test_Base");  
  13.                 dataGrid1.DataSource=ds;  
  14.             }  
  15.             catch (Exception exc)  
  16.             {  
  17.                 MessageBox.Show(exc.Message);  
  18.             }  
  19.         } 

  编辑添加dataGrid以后更新数据

  1. private void button2_Click(object sender, System.EventArgs e)  
  2.         {              
  3.             try  
  4.             {                  
  5.                 //这里ds.Table[0]里面的数据已经改变  
  6.                 string str_Conn="workstation id=JHTCHINA;packet size=4096;user id=sa;initial catalog=master;persist security info=False";  
  7.                 //整体把修改的数据更新到数据库里面  
  8.                 SqlConnection objConn=new SqlConnection(str_Conn);  
  9.                 string str_sql="select * from Test_Base";  
  10.                 SqlCommand objComm=new SqlCommand(str_sql,objConn);   
  11.                 SqlDataAdapter objAdapter=new SqlDataAdapter(objComm);  
  12.                 SqlCommandBuilder updataBulid=new SqlCommandBuilder(objAdapter);  
  13.                 objAdapter.Update(ds,"Test_Base");  
  14.                   
  15.                 MessageBox.Show("OK");  
  16.  
  17.             }  
  18.             catch  (Exception exc)  
  19.             {  
  20.                 MessageBox.Show(exc.Message);  
  21.             }  
  22.         }  

  运行通过

  如果是SQL整体更新添加,在数据读取的时候
string str_sql="select * from Test_Base where 1=2";
一句代码就可以了