111,129
社区成员
发帖
与我相关
我的任务
分享
string str = "server =dsf;uid=2;pwd=55;;database =zz";
SqlConnection con = new SqlConnection(str);
con.Open();
string strl = "select * from ProductName where 产品编号='" + textBox1.Text + "'";
using (SqlCommand cmdstr = new SqlCommand(strl, con))
{
if (Convert.ToString(cmdstr.ExecuteScalar()) == "")
{
StringBuilder strSql = new StringBuilder();
strSql.Append("insert into ProductName values('" + textBox1.Text + "','" + textBox2.Text + "')");
strSql.Append("insert into STOCK(产品编号,总数) values ('" + textBox1.Text + "','0')");
strSql.Append("insert into borrownumber values('" + textBox1.Text + "','0')");
strSql.Append("insert into checktable values('" + textBox1.Text + "','" + textBox3.Text + "')");
try
{
using (SqlCommand cmd = new SqlCommand(strSql.ToString(), con))
{
cmd.ExecuteNonQuery();
MessageBox.Show("添加完成");
textBox1.Text = "";
textBox2.Text = "";
}
}
catch(Exception ex)
{
MessageBox.Show("添加失败");
return;
}
}
大家看看我写的对不?没测试总感觉别扭 #3楼
public static bool ExecuteNonQuery(string cmdText , IList<SqlParameter> parmList)
{
SqlTransaction trans = conn.BeginTransaction("NoQuery");
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = cmdText;
cmd.Transaction = trans;
if (parmList != null)
foreach (SqlParameter parm in parmList)
cmd.Parameters.Add(parm);
cmd.ExecuteNonQuery();
trans.Commit();
return true;
}
catch (Exception ex1)
{
ExceptionHandleOperation(ex1 , null);
try
{
trans.Rollback();
}
catch (Exception ex2)
{
ExceptionHandleOperation(ex2 , null);
}
return false;
}
}
我没有测试