问题如题,winform中,网上很多用SqlCommandBuilder+SqlDataAdapter来实现,还有说用ds.haschanges,请教各位了!最好能详细点,

解决方案 »

  1.   

    首先,我们需要定义一个SqlDataAdapter:SqlDataAdapter thisAdapter = new SqlDataAdapter(strQuery, conn);StrQuery是查询语句,conn是一个SqlConnection,然后定义一个SqlCommandBuilder:SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);这个SqlCommandBuilder用来自动生成添加、删除、修改的语句,注意这个参数是刚才建立的SqlDataAdapter。当上述工作完成之后,我们调用SqlDataAdapter的Fill()方法,将查询出来的数据表内容填充的一张DataTable里面:thisAdapter.Fill(ds, "TableName");ds是一个DataSet,TableName可以自己写,一会Update()函数用的上。之后,对这张DataTable进行一些添加、删除、修改操作,然后调用Update()方法,将这些对DataTable进行的更改批量更新到数据库对应的表中:thisAdapter.Update(ds, "TableName");
      

  2.   

    谢谢三楼,我实现也是这样实现的,但是我发现我点击刷新,并没有发生更新数据库public partial class MainForm : Form
    {
    DBClass  db =new DBClass ();
    SqlConnection  conn;
    SqlDataAdapter sda;
    int intindex =0;
    public MainForm()
    {
    //
    // The InitializeComponent() call is required for Windows Forms designer support.
    //
    InitializeComponent();

    //
    // TODO: Add constructor code after the InitializeComponent() call.
    //
    }

    void MainFormLoad(object sender, EventArgs e)
    {
    conn =db .GetConnection ();
    conn .Open ();
    string  strsql="select * from tb_user";
    //DataTable  dt =new DataTable ();
    db.BindDataGridView (dataGridView1 ,strsql );
    }

    void Button1Click(object sender, EventArgs e)
    {
    string  strsql="select * from tb_user";
    SqlCommand  cmd =db .GetCommandStr (strsql );//获取cmd
    DataTable   dt =db .GetDataSet (cmd,"tb_user" );//填充tb_user表格到dt表格。实际上返回的是一个DataSet
    dt .Rows .Clear ();//清空内存的DataSet数据

    DataTable dtshow =new DataTable ();
    dtshow =(DataTable ) this .dataGridView1 .DataSource ;//加载当前datagridview的数据,包括修改的和没有修改的
    dt.ImportRow(dtshow.Rows[intindex]); //指定的一行数据
    dataGridView2 .DataSource =dt ;
    if (dtshow .RowChanged ())
    {
    SqlCommandBuilder  scb;
    this .sda =new SqlDataAdapter (strsql ,conn );
    scb =new SqlCommandBuilder (sda );
    this.sda .Update (dt);
    dt .AcceptChanges ();
    }
    }

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                intindex = e.RowIndex;
            }
    }
    }
      

  3.   

    http://blog.csdn.net/emailqjc/archive/2010/05/18/5605107.aspx
      

  4.   

    你这段代码里边都没有更新语句,怎么也不可能更新数据库
    请参考这个准备工作,声明窗体级全局变量:        private DataSet ds = new DataSet();
            private SqlDataAdapter sda = new SqlDataAdapter();
            private SqlConnection myConn = new SqlConnection();1、利用SqlCommandBuilder       SqlCommandBuilder sb = new SqlCommandBuilder(sda);
          sda.Update(ds.Tables[0]);
          ds.Tables[0].AcceptChanges();  2、指定UpdateCommand SqlParameter param = new SqlParameter();            string strSql = " update  Good_PrtyInfo set   maxstcs=@maxstcs  ,  minstcs=@minstcs   where 1=1  and GoodID=@GoodID ";
                sda.UpdateCommand = new SqlCommand(strSql, myConn);            param = sda.UpdateCommand.Parameters.Add("@maxstcs", SqlDbType.VarChar, 50, "maxstcs");
                param.SourceVersion = DataRowVersion.Current;            param = sda.UpdateCommand.Parameters.Add("@minstcs", SqlDbType.VarChar, 50, "minstcs");
                param.SourceVersion = DataRowVersion.Current;            //param = myDA.UpdateCommand.Parameters.Add("@DeptID", SqlDbType.VarChar, 50, "DeptID");
                //param.SourceVersion = DataRowVersion.Current;            param = sda.UpdateCommand.Parameters.Add("@GoodID", SqlDbType.VarChar, 50, "GoodID");
                param.SourceVersion = DataRowVersion.Original;            sda.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;            sda.UpdateBatchSize = 10;
                //SqlCommandBuilder sb = new SqlCommandBuilder(sda);
                sda.Update(ds.Tables[0]);
                ds.Tables[0].AcceptChanges();  本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/emailqjc/archive/2010/05/18/5605107.aspx