保存修改语句的应用

weixin_41952410 2018-04-19 02:50:34
为何这样只能修改,不能插入保存。
private void button1_Click_1(object sender, EventArgs e)
{
if (txtxmmc.Text == string.Empty)
{
MessageBox.Show("信息不完整!");
txtxmmc.Focus();
return;
}
bool isNew = false;
string sql = "SELECT xmid FROM KHXM WHERE xmid=@A ";
SqlParameter[] pm = { new SqlParameter("@A", SqlDbType.VarChar, 200) };
pm[0].Value = txtxmmc.Text.Trim();
DataTable a = DbHelperSQL.ExecuteReader(sql, pm);
if (a == null)
{
isNew = true;
}
else
{
if (a.Rows.Count > 0)
{
isNew = false;
MessageBox.Show(a.Rows[0]["xmmc"].ToString());
}
else
{
isNew = true;
}
}
StringBuilder yzt = new StringBuilder();
// SqlParameter[] a=null;
int id = (int)txtxmmc.Tag;
//int id = DbHelperSQL.GetTableKID("KHXM");
if (id<1)
{
isNew = true;
id = DbHelperSQL.GetTableKID("KHXM");
yzt.Append("INSERT INTO KHXM(xmid,xmmc,khmc,fwmd,lxr,lxdh,dz,yb,dh,cz,mail,xmsm,yx)VALUES");
yzt.Append("(@A,@B,@C,@D,@E,@F,@G,@H,@I,@J,@K,@L,@M)");

}
else
{
yzt.Append("UPDATE KHXM SET xmmc=@B,khmc=@C,fwmd=@D,lxr=@E,lxdh=@F,dz=@G,yb=@H,dh=@I,cz=@J,mail=@K,xmsm=@L,yx=@M WHERE xmid=@A ");
}
SqlParameter[] c = { new SqlParameter("@A",SqlDbType.Int,4),
new SqlParameter("@B",SqlDbType.VarChar,200),
new SqlParameter("@C",SqlDbType.VarChar,200),
new SqlParameter("@D",SqlDbType.VarChar,100),
new SqlParameter("@E",SqlDbType.VarChar,50),
new SqlParameter("@F",SqlDbType.VarChar,50),
new SqlParameter("@G",SqlDbType.VarChar,300),
new SqlParameter("@H",SqlDbType.VarChar,10),
new SqlParameter("@I",SqlDbType.VarChar,50),
new SqlParameter("@J",SqlDbType.VarChar,50),
new SqlParameter("@K",SqlDbType.VarChar,50),
new SqlParameter("@L",SqlDbType.VarChar,1000),
new SqlParameter("@M",SqlDbType.Bit),
};
c[0].Value = id;
c[1].Value = txtxmmc.Text;
c[2].Value = txtkhmc.Text;
c[3].Value = txtfwmd.Text;
c[4].Value = txtlxr.Text;
c[5].Value = txtlxdh.Text;
c[6].Value = txtdz.Text;
c[7].Value = txtyb.Text;
c[8].Value = txtdh.Text;
c[9].Value = txtcz.Text;
c[10].Value = txtmail.Text;
c[11].Value = txtxmsm.ToString();
if (checkBox1.Checked == true)
{ c[12].Value = true; }
else
{ c[12].Value = false; }
int d = DbHelperSQL.ExecuteSql(yzt.ToString(), c);
if (d > 0)
{

MessageBox.Show("保存成功!");

}
else
{
MessageBox.Show("保存失败!");
}
}
}
}
...全文
400 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 2018-04-19
  • 打赏
  • 举报
回复
因为有 if (id<1) 判断,所以可能你走不到 insert 的条件 这种自己调试就好了。

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧