为什么此代码插入具有不同ID的重复行 [英] why this code insert duplicate rows with different id
本文介绍了为什么此代码插入具有不同ID的重复行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我插入新行的功能
Hi,
this is my function to insert new row
public int addrow(string sqlcommand)
{
int res = 0;
try
{
con.Open();
cmd.CommandText = sqlcommand;
cmd.Connection = con;
cmd.ExecuteNonQuery();
if (cmd.ExecuteNonQuery() == 1)
res = 1;
}
catch (Exception exp)
{
throw exp;
}
finally
{
con.Close();
}
return res;
}
当我调用此函数时,它插入了两行
when i call this function it inserted twice rows
string command = "insert into caseT values(N'" + caseTxt.Text + "')";
int addRow = db.addrow(command);
if (addRow == 1)
{
MessageBox.Show("inserted");
caseTxt.Text = "";
fillCase();
}
1 case1
2 case2
任何帮助
1 case1
2 case2
any help
推荐答案
因为你叫它两次:
Because you call it twice:
cmd.ExecuteNonQuery();
if (cmd.ExecuteNonQuery() == 1)
res = 1;
更改为:
Change it to:
int res = cmd.ExecuteNonQuery();
这篇关于为什么此代码插入具有不同ID的重复行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文