写了一个保存的语句。当不存在danh时,用insert插入记录,当存在danh时,执行update。为什么没错执行update时,只能把dataGridView1表最后的一条数据全部更新了?,第一次保存是insert的。第2次点保存是update时出错,全部替代为一条数据了。本来是保存两条不同的数据的,为什么update时,两条更新为dataGridView1里最后的一条了。百度问问的链接里有图片http://zhidao.baidu.com/question/448991880.html?quesup2下面是保存的的代码,我要的点保存时就把数据保存起来,当有数据更新时(包刮增加有新数据时),把原来的覆盖了或更新了原来有的数据,再把新增的数据增加到数据库。请高手帮个改进和完善。
                dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[2];
                string str = "server=.;database=sjzlcrmdb;uid=sa;pwd=1109";
                SqlConnection con = new SqlConnection(str);
                int b = this.dataGridView1.RowCount; //计算行数
                int z = b - 1; //减一,因为是从行记起
                con.Open();
               
   
                
                SqlCommand cmd3 = new SqlCommand("select * from SJZL_ERP_BCPRKD where danh='"+label4.Text+"'",con );
                SqlDataReader sdr3 = cmd3.ExecuteReader();
                sdr3.Read();
                if (sdr3.HasRows)
                {
                    for (int y = 0; y < z; y++)
                    {
                        //------保存到SJZL_ERP_BCPRK 半成品入库明细表----
                        sdr3.Close();
                        string rk = "update SJZL_ERP_BCPRK set tm='" + dataGridView1.Rows[y].Cells[1].Value + "',bcpmc='" + dataGridView1.Rows[y].Cells[3].Value + "',shul='" + dataGridView1.Rows[y].Cells[4].Value + "',jt='" + dataGridView1.Rows[y].Cells[5].Value + "',jtbm='" + dataGridView1.Rows[y].Cells[6].Value + "',guig='" + dataGridView1.Rows[y].Cells[7].Value + "',leib='" + dataGridView1.Rows[y].Cells[8].Value + "',zhongl='" + dataGridView1.Rows[y].Cells[9].Value + "',qita='" + textBox5.Text.Trim() + "'";
                        SqlCommand cmd = new SqlCommand(rk, con);
                        cmd.ExecuteNonQuery();                    }                        //------保存到SJZL_ERP_BCPRKD  半成品入库单表
                        string rkd = "update SJZL_ERP_BCPRKD set jbr='" + textBox2.Text.Trim() + "',kdsj='" + textBox1.Text.Trim() + "',kdbm='" + textBox6.Text.Trim() + "',bmbm='"+Bumendaima1 +"',shck='"+textBox3 .Text .Trim ()+"',ckbm='"+cangku1 +"',jine='"+textBox4 .Text .Trim ()+"',suod='保存',qita='"+textBox5 .Text .Trim ()+"' where danh='"+label4 .Text +"'";
                        SqlCommand cmd2 = new SqlCommand(rkd , con);
                        cmd2.ExecuteNonQuery();
                                   
                }
                else
                {
                    for (int y = 0; y < z; y++)
                    {                        //------保存到SJZL_ERP_BCPRK 半成品入库明细表----
                        sdr3.Close();
                        string rk = "insert into SJZL_ERP_BCPRK(tm,bcpmc,shul,jt,jtbm,guig,leib,zhongl,qita,danh) values ('" + dataGridView1.Rows[y].Cells[1].Value + "','" + dataGridView1.Rows[y].Cells[3].Value + "','" + dataGridView1.Rows[y].Cells[4].Value + "','" + dataGridView1.Rows[y].Cells[5].Value + "','" + dataGridView1.Rows[y].Cells[6].Value + "','" + dataGridView1.Rows[y].Cells[7].Value + "','" + dataGridView1.Rows[y].Cells[8].Value + "','" + dataGridView1.Rows[y].Cells[9].Value + "','" + textBox5.Text.Trim() + "','" + label4.Text + "')";
                        SqlCommand cmd = new SqlCommand(rk, con);
                        cmd.ExecuteNonQuery();                    }
                        //------保存到SJZL_ERP_BCPRKD  半成品入库单表
                        string rkd = "insert into SJZL_ERP_BCPRKD(jbr,danh,kdsj,kdbm,bmbm,shck,ckbm,jine,suod,qita) values ('" +textBox2 .Text .Trim ()+ "','"+label4 .Text +"','"+textBox1 .Text .Trim ()+"','"+textBox6 .Text .Trim ()+"','"+Bumendaima1 +"','"+textBox3 .Text .Trim ()+"','"+cangku1 +"','"+textBox4 .Text .Trim ()+"','保存','"+textBox5 .Text .Trim ()+"')";
                        SqlCommand cmd2 = new SqlCommand(rkd , con);
                        cmd2.ExecuteNonQuery();
                        
                    
                }

解决方案 »

  1.   

    高手啊,真有耐心,竟然拼了4段sql语句。
    不过我没耐心看,不好意思了。
      

  2.   

    建议复制代码的时候引用相应代码的模块Dataset ds = new Dataset();
      

  3.   

    你更新写了多少条sql语句rk  rkd都执行了的先把逻辑理清楚再写代码
      

  4.   

    短的描述在百度问问里http://zhidao.baidu.com/question/448991880.html?quesup2
      

  5.   

    http://blog.csdn.net/xianfajushi/article/details/7707042
      

  6.   

    是使用StringBuilder来拼接SQL字符串