请问用c#访问sql数据库时候,adapter的update方法怎么用啊,我搞了好几遍都没搞懂,我是个初学者,还望各位大神不吝赐教啊

解决方案 »

  1.   

            private DataTable DT = new DataTable();
            private SqlDataAdapter SDA = new SqlDataAdapter();
            SqlConnection conn = new SqlConnection(DBClass.ConnStr);
            public datagreadForm()
            {
                InitializeComponent();
            }        private void datagreadForm_Load(object sender, EventArgs e)
            {
                button5.Enabled = false;
            }        private void selectbutton_Click(object sender, EventArgs e)
            {
                DT.Clear();
                string sqlstr = "select PlateNumber, goodsname,inputtime, grossweight, thetare, netweight, sunit, runit,  biaoji from daoyuntable where biaoji=0 and PlateNumber='" + textBox1.Text
                    + "' and goodsname='" + textBox2.Text + "' and sunit='" + textBox3.Text + "' and runit='"+textBox4.Text+"' and bfname='"+DBClass.bangfangStr+"'";
                
                SqlCommand SCD = new SqlCommand(sqlstr, conn);
                SDA.SelectCommand = SCD;
                SDA.Fill(DT);
                dataGridView1.DataSource = DT;
                button5.Enabled = true;
            }        private void button5_Click(object sender, EventArgs e)
            {
                int k = 0;
                float a = 0;
                if (dataGridView1.RowCount > 0) 
                {
                    for (int i = 0; i < dataGridView1.RowCount; i++)
                    {                    if (bool.Parse(dataGridView1.Rows[i].Cells[8].Value.ToString()))
                        {
                            k = k + 1;
                            a=a+float.Parse(dataGridView1.Rows[i].Cells[3].Value.ToString());
                        }
                    }
                    textBox5.Text = dataGridView1.Rows[0].Cells[0].Value.ToString();
                    textBox6.Text = dataGridView1.Rows[0].Cells[2].Value.ToString();
                    textBox7.Text = a.ToString();//总的毛重数
                    textBox8.Text = k.ToString();//记录修改的记录数               //label2.Text = "你选择了" + k.ToString() + "条记录  总的毛重是:"+a.ToString();
                    //MessageBox.Show( "你选择了"+k.ToString()+"条记录");
                }
                try
                { 
                    SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);
                    SDA.Update(DT);
                }
                catch (System.Exception ex) 
                {
                    MessageBox.Show(ex.ToString()); 
                    return; 
                }
                MessageBox.Show("更新成功!");
                button5.Enabled = false;        }        private void button1_Click(object sender, EventArgs e)
            {
                chehaoForm dlg = new chehaoForm(textBox1.Text);
                dlg.ShowDialog();
                textBox1.Text = dlg.chehaoValue;
            }        private void button2_Click(object sender, EventArgs e)
            {
                appClass.IDStr = 2;
                unitForm dlg = new unitForm(textBox2.Text);
                dlg.ShowDialog();            textBox2.Text = dlg.unitValue;
            }        private void button3_Click(object sender, EventArgs e)
            {
                appClass.IDStr = 1;
                unitForm dlg = new unitForm(textBox3.Text);
                dlg.ShowDialog();
                textBox3.Text = dlg.unitValue;
            }        private void button4_Click(object sender, EventArgs e)
            {
                appClass.IDStr = 1;
                unitForm dlg = new unitForm(textBox4.Text);
                dlg.ShowDialog();
                textBox4.Text = dlg.unitValue;
            }
      

  2.   

    简言之,为Adapter实现CUD操作所需的Command是先决条件。大致就是下面这个样子。不过可以用CommandBuilder省一些事。
    string sql = @".......";
    adapter = new adapter(sql, conn);
    adapter.updatecommand = new command();
    adapter.insertcommand = new command();
    adapter.deletecommand = new command()..... .....adapter.Update();
      

  3.   

    偶还是觉得用Linq To SQL访问数据库比较简单...