写了一个保存的语句。当不存在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();
}
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();
}
解决方案 »
- C#怎样实现类似“遨游浏览器标签”的功能
- [肯定有错] abstract static string strName{get;set;}
- 求助:C#应用程序中数据库操作问题
- 很迷茫,希望大家帮忙!
- 关于ExcelQuicker 根据不同模板生成多个worksheet的请教
- form程序启动的时候报错
- .NET下您梦寐以求的DataGrid组件(免费使用且开源哦)
- C# 和 .NET 内存消耗及性能问题,高分求证!
- 如何用GDI+实现GDI重的CDC::SetROP2函数的功能
- 请问C#开发的组件,在没有.net 环境下,VB6可以调用吗?
- 【那个好心人帮我看看啊?】使用XmlReader类读取XML文件????
- 统计数组非0的值,有不懂的地方,望高手解答!
不过我没耐心看,不好意思了。