当我输入下拉列表参数作为数据时出现错误,我无法将参数数据添加到发票表中,小计,税和总计的参数数据得到了自己的值,但它只适用于手动输入数据。
下拉列表参数:
using (var cmd = con.CreateCommand())
{
cmd.CommandText = @"insert into Invoice(subtotal,tax,total) values (@subtotal,@tax,@total); select SCOPE_IDENTITY() as invoiceID;";
cmd.Parameters.AddWithValue("@subtotal", subtotal);
cmd.Parameters.AddWithValue("@tax", tax);
cmd.Parameters.AddWithValue("@total", total);
object OBJinvoiceID = cmd.ExecuteScalar();
}
手动输入:
using (var cmd = con.CreateCommand())
{
cmd.CommandText = @"insert into Invoice(subtotal,tax,total) values (2,2,2); select SCOPE_IDENTITY() as invoiceID;";
object OBJinvoiceID = cmd.ExecuteScalar();
}
答案 0 :(得分:3)
只需从您的值列表中删除字段:
insert into Invoice(subtotal,tax,total) values (@subtotal,@tax,@total); select SCOPE_IDENTITY() as invoiceID;
您还可以使用OUTPUT-Clause获取值:
into Invoice(subtotal,tax,total) OUTPUT invoiceID values (@subtotal,@tax,@total);
如果您确实想手动设置identity
- 列,可以按照here in MSDN所述使用SET IDENTITY_INSERT
答案 1 :(得分:0)
请勿设置invoiceID
insert into Invoice(subtotal,tax,total) values (@subtotal,@tax,@total);
答案 2 :(得分:0)
如果你在数据库中有增量,你不需要在代码中再次改变它。只需从您的对帐单中删除invoiceID
即可答案 3 :(得分:0)
我不是.NET的人,但通过谷歌搜索,我认为你不必在插入查询中提到身份字段:
using (var cmd = con.CreateCommand())
{
cmd.CommandText = @"insert into Invoice(subtotal,tax,total) values (@subtotal,@tax,@total);
// the rest
}
Plz看看这个简单的教程: w3schools
答案 4 :(得分:0)
insert into Invoice(subtotal,tax,total) values (@subtotal,@tax,@total); select SCOPE_IDENTITY() as invoiceID;