保存修改语句的应用
为何这样只能修改,不能插入保存。
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("保存失败!");
}
}
}
}