-3

我有一张桌子,materials和另一个,stock_refund。我在单击一次按钮时执行这两个命令以同时更新两个表,但两个记录都进入同一个表。以下是我的代码:

SqlConnection con = new SqlConnection(@"Data Source=ANONYMOUS\SQLSERVER;Initial Catalog=IndoStar;Integrated Security=SSPI;");
con.Open();
SqlCommand cmd = new SqlCommand("Insert into stock_refund values('"+refund_stockname.Text + "','"+refund_sitename.Text + "','"+refund_day.Text + refund_month.Text + refund_year.Text + "','"+refund_quantity.Text + "','"+ refund_unit.Text+"')",con);
SqlCommand cmd1 = new SqlCommand("Insert into materials values('" + refund_stockname.Text + "','" + refund_day.Text + refund_month.Text + refund_year.Text + "','" + refund_quantity.Text + "','" + refund_unit.Text + "', 'refund','"+ refund_sitename.Text +"')", con);
int i = cmd.ExecuteNonQuery();
int j = cmd.ExecuteNonQuery();
if ((i > 0)&& (j>0))
{
    MessageBox.Show("Stock refunded successfully","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
4

1 回答 1

3

看来你这里有一个错字:

int i = cmd.ExecuteNonQuery();
int j = cmd.ExecuteNonQuery();

应该

int i = cmd.ExecuteNonQuery();
int j = cmd1.ExecuteNonQuery();

您正在执行第一个命令两次。

于 2012-08-04T16:54:40.690 回答