using PreSqlData.DataProcess;

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. using System.Data.SqlClient;  
  11.   
  12.   
  13. namespace AllSaveGrid  
  14. {  
  15.     public partial class Form1 : Form  
  16.     {  
  17.   
  18.         DataSet ds = new DataSet();  
  19.           
  20.         public Form1()  
  21.         {  
  22.             InitializeComponent();  
  23.             GridControl_Bind();  
  24.         }  
  25.         public void GridControl_Bind()  
  26.         {  
  27.             SqlClass sc = new SqlClass();  
  28.             //DataSet ds = new DataSet();  
  29.             ds = sc.SelTable();                             //获取DataGridView的数据源  
  30.             dataGridView1.DataSource = ds.Tables[0];  
  31.         }  
  32.   
  33.         private void button1_Click(object sender, EventArgs e)  
  34.         {  
  35.   
  36.             SqlConnection con = new SqlConnection("server='localhost';database='Work';user='sa';password='123'");  
  37.             SqlCommand cmd = new SqlCommand();  
  38.             cmd = con.CreateCommand();  
  39.             cmd.CommandText = "select * from Table_gongzi";  
  40.             SqlDataAdapter da = new SqlDataAdapter(cmd);  
  41.   
  42.             SqlCommandBuilder cb = new SqlCommandBuilder(da);//使用SqlCommandBuilder的话,首先要求你的表有主键   
  43.                                                              //然后他会根据你提供的查询语句,生成插入,修改,删除语句  
  44.                                                              //再根据DataTable对应的数据行状态来调用对应的语句执行数据库操作  
  45.             da.Update(ds.Tables[0]);  
  46.             dataGridView1.Update();  
  47.         }  
  48.   
  49.         private void button2_Click(object sender, EventArgs e)  
  50.         {  
  51.             DataRowView drv = dataGridView1.SelectedRows[0].DataBoundItem as DataRowView;  
  52.             drv.Delete();  
  53.               
  54.             SqlConnection con = new SqlConnection("server='localhost';database='Work';user='sa';password='123'");  
  55.             SqlCommand cmd = new SqlCommand("select * from Table_gongzi", con);  
  56. //          SqlCommand cmd = new SqlCommand();  
  57. //          cmd = con.CreateCommand();  
  58. //          cmd.CommandText = "select * from Table_gongzi";  
  59.   
  60.             SqlDataAdapter da = new SqlDataAdapter(cmd);  
  61.             SqlCommandBuilder cb = new SqlCommandBuilder(da);//必须new SqlCommandBuilder 否则da.Update报错  
  62.             da.Update(ds.Tables[0]);  
  63.             dataGridView1.Update();  
  64.         }  
  65.     }  

 

转载声明: 本文转自 http://blog.csdn.net/shanzhaikaifa/archive/2009/08/13/4443931.aspx

 

posted @ 2010-03-20 17:33  程序员天下  阅读(146)  评论(0编辑  收藏  举报