private void btnAdd_Click(object sender, EventArgs e)
        {
            //string select1="insert into tb_user values (        }        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || cboxUName.Text == "" || txtUPwd.Text == "" || cboxURight.Text == "" )
                MessageBox.Show("请输入相应的值!");            string select2 = "update tb_User set UserName= '" + cboxUName.Text + "', StorePeople='" + txtUPwd.Text + "',  StorePhone='" + cboxURight.Text + "',  StoreUnit=' ";
            comm = new SqlCommand(select2, conn);
            conn.Open();
            comm.ExecuteNonQuery();
            ds = new DataSet();
            da.Fill(ds);
            this.dgvUInfo.DataSource = ds.Tables[0];
            MessageBox.Show("命令执行成功!");
            return;
        }        private void btnDel_Click(object sender, EventArgs e)
        {
            int a = 0;
            try { a = int.Parse(textBox1.Text); }
            catch (Exception ee) { MessageBox.Show(ee.Message); return; }
            string delete = "delete from tb_User where UserID=  " + textBox1.Text + "  ";
            comm = new SqlCommand(delete, conn);
            conn.Open();
            comm.ExecuteNonQuery();
            ds = new DataSet();
            da.Fill(ds);
            this.dgvUInfo.DataSource = ds.Tables[0];
            conn.Close();

解决方案 »

  1.   

    就是SQL语句不同而已,如果上面真的是你自己做的,那么你自己修改下SQL语句还修改不了?修改也就是update而已!
      

  2.   

    基本上模式都差不多
    //這是例子,插入語句,插入前查詢數據庫有無數據,有就更新,沒有則插入
    //string delSql = "delete from table where 條件";
    //我這個是oracle數據庫的 sql的就把oraclecommand改成相應的sqlcommand就好
    string insertSql = "DECLARE  num   NUMBER;BEGIN SELECT COUNT (*) INTO num  FROM glb_rpt_board WHERE start_time = '" + stime + "';IF num <> 0 THEN UPDATE glb_rpt_board SET business = '" + textBoxBusiness.Text.Trim() + "', model = '" + textBoxLine.Text.Trim() + "', input_step = '" + dt.Rows[i][0] + "', output_step ='" + dt.Rows[i][1] + "',end_time = '" + etime + "',otime = " + Convert.ToDecimal(dt.Rows[i][4]) +
                                    @", input ="+Convert.ToDecimal(dt.Rows[i][5])+@",output = "+Convert.ToDecimal(dt.Rows[i][6])+@",fpy = "+Convert.ToDecimal(dt.Rows[i][7])+@",uph = "+Convert.ToDecimal(dt.Rows[i][8])+@",upph ="+Convert.ToDecimal(dt.Rows[i][9])+@" where start_time = '"+stime+ @"';ELSE INSERT INTO glb_rpt_board VALUES ('" + textBoxBusiness.Text.Trim() + @"', '" + textBoxLine.Text.Trim() + @"', '" + dt.Rows[i][0] + 
                                    @"', '" + dt.Rows[i][1] + @"','" + stime + @"','" + etime +@"',"+Convert.ToDecimal(dt.Rows[i][4])+@","+Convert.ToDecimal(dt.Rows[i][5])+@","+Convert.ToDecimal(dt.Rows[i][6])+@","+Convert.ToDecimal(dt.Rows[i][7])+@","+Convert.ToDecimal(dt.Rows[i][8])+@","+Convert.ToDecimal(dt.Rows[i][9])+"); END IF;END;";
                    OracleCommand insertCmd = conn.CreateCommand();
                    insertCmd.CommandText = insertSql;
                    insertCmd.ExecuteNonQuery();
    其中的數據庫語句
    /* Formatted on 2013/06/13 09:19 (Formatter Plus v4.8.8) */
    DECLARE
       num   NUMBER;
    BEGIN
       SELECT COUNT (*)
         INTO num
         FROM glb_rpt_uph
        WHERE start_time = TO_DATE ('2013-6-13 15:30:00', 'yyyy-MM-dd hh24:mi:ss');   IF num <> 0
       THEN
          UPDATE glb_rpt_uph
             SET model = 'AK73-BTE-EA1',
                 shift = 'd',
                 end_time =
                           TO_DATE ('2013-6-13 16:30:00', 'yyyy-MM-dd hh24:mi:ss'),
                 input = 0,
                 output = 0,
                 fpy = 1,
                 uph = 107,
                 upph = 4
           WHERE start_time =
                           TO_DATE ('2013-6-13 15:30:00', 'yyyy-MM-dd hh24:mi:ss');
       ELSE
          INSERT INTO glb_rpt_uph
               VALUES ('AK73-BTE-EA1', 'd',
                       TO_DATE ('2013-6-13 15:30:00', 'yyyy-MM-dd hh24:mi:ss'),
                       TO_DATE ('2013-6-13 16:30:00', 'yyyy-MM-dd hh24:mi:ss'),
                       0, 0, 1, 107, 4);
       END IF;
    END;
    拋去字段部份,格式都一樣,自己根據實際需要修改吧