数据无法通过c#进入数据库 [英] Data are unable to enter in database through c#
本文介绍了数据无法通过c#进入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void toolStripButton3_Click(object sender, EventArgs e)
{
if (textBox14.Text.Equals(textBox15.Text))
{
try
{
SqlCommand sqlcmd = sqlconn.CreateCommand();
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
sqlcmd.CommandText = "set identity_insert gl_Transaction On";
sqlcmd.CommandText = "insert into Gl_Transaction(Compcode,Branchcode,Accountno,Acct_Name,SerialNo,Acct_Nirration,Value_Date,Voucher_No,Vchrtype,Dr_Amount,Cr_Amount,AddDate,UserId,pflag,InstrumentNo) values('" + dr.Cells[0].Value.ToString() + "','" + dr.Cells[1].Value.ToString() + "','" + dr.Cells[2].Value.ToString() + "','" + dr.Cells[3].Value.ToString() + "','" + dr.Cells[4].Value.ToString() + "','" + dr.Cells[5].Value.ToString() + "','" + dr.Cells[6].Value.ToString() + "','" + dr.Cells[7].Value.ToString() + "','" + dr.Cells[8].Value.ToString() + "','" + dr.Cells[9].Value.ToString() + "','" + dr.Cells[10].Value.ToString() + "','" + dr.Cells[11].Value.ToString() + "','" + dr.Cells[12].Value.ToString() + "','" + dr.Cells[13].Value.ToString() + "','" + dr.Cells[14].Value.ToString() + "')";
sqlcmd.CommandText = "set identity_insert gl_Transaction Off";
try
{
sqlcmd.ExecuteNonQuery();
}
catch (SqlException err)
{
MessageBox.Show(err.Message);
}
MessageBox.Show("Record Entered Successfull");
}
}
catch (Exception)
{
throw;
}
}
else
{
MessageBox.Show("Debit and credit are not equal");
}
此代码中没有错误...但数据不是进入表...错误的错误...
i我只需点击一下就可以通过gridview两行发送deata ...
There is no error in this code... but data are not going in table... wats the error...
i am sending deata through gridview two rows at a single click...
推荐答案
尝试以下方法:
Try the following :
...
sqlcmd.CommandText = "set identity_insert gl_Transaction On;";
sqlcmd.CommandText += "insert into Gl_Transaction(Compcode,Branchcode,Accountno,Acct_Name,SerialNo,Acct_Nirration,Value_Date,Voucher_No,Vchrtype,Dr_Amount,Cr_Amount,AddDate,UserId,pflag,InstrumentNo) values('" + dr.Cells[0].Value.ToString() + "','" + dr.Cells[1].Value.ToString() + "','" + dr.Cells[2].Value.ToString() + "','" + dr.Cells[3].Value.ToString() + "','" + dr.Cells[4].Value.ToString() + "','" + dr.Cells[5].Value.ToString() + "','" + dr.Cells[6].Value.ToString() + "','" + dr.Cells[7].Value.ToString() + "','" + dr.Cells[8].Value.ToString() + "','" + dr.Cells[9].Value.ToString() + "','" + dr.Cells[10].Value.ToString() + "','" + dr.Cells[11].Value.ToString() + "','" + dr.Cells[12].Value.ToString() + "','" + dr.Cells[13].Value.ToString() + "','" + dr.Cells[14].Value.ToString() + "')";
sqlcmd.CommandText += ";set identity_insert gl_Transaction Off";
...
如果你需要使用复合sql语句,即超过1,然后每个应该以';'分号结束。
同样在上面的情况下你需要连接 CommandText的字符串
+ =
否则会被覆盖。
这篇关于数据无法通过c#进入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文