answer
在结束阅读之前清除textBox是一个错误。
感谢@ muhammad-azim
结束答案
我首先在数据库中插入了2个,第二个在第一个中具有外键。
执行第二个INSERT
时出现错误:
INSERT语句与FOREIGN KEY约束冲突
这部分出错了
代码:
SqlConnection con1 = new SqlConnection(connectionString);
SqlCommand cmd1 = new SqlCommand(cmdString, con1);
con1.Open();
cmd1.ExecuteNonQuery(); // insert(987,10000,2019-07-27 12:00:00 AM)
con1.Close();
// Some Code
SqlConnection con2 = new SqlConnection(connectionString);
SqlCommand cmd2 = new SqlCommand(cmdString2, con2);
con2.Open()
cmd2.ExecuteNonQuery();//want insert(1,987,2000,2019-07-28 12:00:00 AM) but Got error
con2.Close();
两个查询均为INSERT
我认为Connection不理解数据库已更新并出现此错误。 当我停止应用程序并单独运行另一个第二次查询时,它就可以工作了(以前没用过)
所有代码
PersianCalendar pc = new PersianCalendar();
string[] dateInfo = txtcreteDate.Text.Split('-');
DateTime createDate = new DateTime(Convert.ToInt32(dateInfo[0]), Convert.ToInt32(dateInfo[1]), Convert.ToInt32(dateInfo[2]), pc);
int companyid = 1;
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("INSERT INTO Factors (compId,createDate, code, duration, value," +
" discount , payType , payTypeInfo , status)" +
"VALUES " +
" (@compId,@createDate, @code, @duration, @value, @discount , @payType , @payTypeInfo , 1) ", con);
cmd.Parameters.Add("@compId", companyid);
cmd.Parameters.Add("@createDate", createDate);
cmd.Parameters.Add("@code", txtCode.Text);
cmd.Parameters.Add("@duration", Convert.ToInt32(txtDuration.Text));
cmd.Parameters.Add("@value", Convert.ToDecimal(txtValue.Text));
cmd.Parameters.Add("@discount", Convert.ToDecimal(txtDiscount.Text));
cmd.Parameters.Add("@payType", companyid);
cmd.Parameters.Add("@payTypeInfo", companyid);
con.Open();
try
{
var insertedItem = cmd.ExecuteNonQuery();
}
catch(Exception ez)
{
MessageBox.Show("شماره فاکتور تکراری است");
return;
}
ClearTextBoxes();
if (comboBox1.SelectedItem.ToString() == "نقد" && insertedItem >0)
{
for (int counter1 = 0; counter1 < cashDataGridView.Rows.Count; counter1++)
{
string status = "پرداخت نشده";
PersianCalendar pc1 = new PersianCalendar();
string[] dateInfoPay = cashDataGridView.Rows[counter1].Cells["date"].Value.ToString().Split('-');
DateTime PayDate = new DateTime(Convert.ToInt32(dateInfoPay[0]), Convert.ToInt32(dateInfoPay[1]), Convert.ToInt32(dateInfoPay[2]), pc1);
//System.Data.SqlClient.SqlConnection con1 = new System.Data.SqlClient.SqlConnection(connectionString);
string cmdString = "INSERT INTO Payout (value,date,status,description,fid)";
cmdString += " VALUES ";
cmdString += " ( '" + Convert.ToDecimal(cashDataGridView.Rows[counter1].Cells["value"].Value.ToString()).ToString() + "' ,";
cmdString += " '" + PayDate.ToString() + "' , ";
cmdString += " '" + status.ToString() + "' , ";
cmdString += " '" + status.ToString() + "' , ";
cmdString += " '" + txtCode.Text + "' )";
System.Data.SqlClient.SqlCommand cmd1 = new System.Data.SqlClient.SqlCommand(cmdString, con);
MessageBox.Show(cmd1.CommandText.ToString());
cmd1.ExecuteNonQuery(); // i got error here
}
con.Close();
答案 0 :(得分:0)
您在这里遇到问题:
SqlConnection con1 = new SqlConnection(connectionString);
SqlCommand cmd1 = new SqlCommand(cmdString, con1);
con1.Open();
cmd1.ExecuteNonQuery();
con1.Close();
ExecuteNonQuery()
返回受影响的行数,如果没有受影响的行,则返回值为-1
并回滚到该操作。
为了确保插入成功并且两个操作都成功完成,您应该始终使用vlidations:
try
{
SqlConnection con1 = new SqlConnection(connectionString);
SqlCommand cmd1 = new SqlCommand(cmdString, con1);
con1.Open();
var rowsAffected = cmd1.ExecuteNonQuery();
con1.Close();
//Some Code
if(rowsAffected > 0)
{
SqlConnection con2 = new SqlConnection(connectionString);
SqlCommand cmd2 = new SqlCommand(cmdString2, con2);
con2.Open()
cmd2.ExecuteNonQuery();
con2.Close();
}
}
catch(Exception ex)
{
//handle error in insert
}
另请参阅SqlCommand.ExecuteNonQuery() returns -1 when doing Insert / Update / Delete
答案 1 :(得分:0)
您不需要使用两个连接对象。
using(SqlConnection con1 = new SqlConnection(connectionString))
{
using(SqlCommand cmd1 = new SqlCommand(cmdString, con1))
{
con1.Open();
//ExecuteNonQuery Method will return 0 or 1.
bool result = Convert.ToBool(cmd1.ExecuteNonQuery());
If insert is succsessful, you can try to execute the second query.
if(result)
{
If doesn't work, you should check your "Some Code" to be sure if you assign the the right parameters.
//Some Code
cmd1.CommandText = cmdString2;
cmd1.ExecuteNonQuery();
}
}
}