2015-04-01 69 views
0

当我输入下拉列表参数作为数据时,我得到一个错误,我无法将参数数据添加到发票表,小计,税收和总计的参数数据他们自己的价值,但它只适用于手动输入数据。如何编写查询自动生成ID

下拉列表参数:

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(); 
} 

手动输入:

insert into Invoice(subtotal,tax,total) values (@subtotal,@tax,@total); select SCOPE_IDENTITY() as invoiceID; 

您也可以使用输出 - :

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

MySQL或Microsoft SQL? – 2015-04-01 10:01:06

+0

如果设置了标识列,则不能指定值。从插入中省略它,它将起作用。 – Kami 2015-04-01 10:01:28

+2

我在这里错过了什么,或者你是否插入了与正在创建的表不同的表格? – colmde 2015-04-01 10:06:15

回答

3

刚刚从你的价值观上市去除场条款获得使用价值:

into Invoice(subtotal,tax,total) OUTPUT invoiceID values (@subtotal,@tax,@total); 

在这种情况下你真的想设置identity -column手动,您可以使用SET IDENTITY_INSERT描述here in MSDN

+0

是的,我已经尝试过,但我得到一个错误,说:“不能插入NULL值列'invoiceID',表;列不允许NULL,INSERT失败。 – qwe 2015-04-02 00:55:51

+0

错误发生在行“object OBJinvoiceID = cmd.ExecuteScalar();” – qwe 2015-04-02 01:00:42

+0

名为'Invoice'或'Order1'的表格是?有区别吗?当你的表有'invoiceID'列作为标识列时,它不能被插入,所以它必须被省略。请检查表格配置和IDENTITY_INSERT设置。 – 2015-04-02 06:49:24

0

不要设置invoiceID

insert into Invoice(subtotal,tax,total) values (@subtotal,@tax,@total); 
+0

是的,我已经尝试过,但我得到一个错误,说:“不能将值NULL插入列'invoiceID',表;列不允许NULL,INSERT失败。 – qwe 2015-04-02 00:56:23

+0

错误发生在“对象OBJinvoiceID = cmd”行。ExecuteScalar();“ – qwe 2015-04-02 01:01:19

+0

我看到你已经编辑了你的帖子,向我们显示了创建发票表的代码,它没有'invoiceID'作为Identity列,这就是为什么你会得到这个错误。 – colmde 2015-04-02 07:12:54

0

如果你拥有了它在数据库递增你不需要在代码中再次修改它。刚刚从你的陈述

+0

yes,I之前尝试过,但是我得到一个错误,说:“不能将值NULL插入列'invoiceID',表;列不允许有空值。 INSERT失败。“ – qwe 2015-04-02 00:56:08

+0

错误发生在行”对象OBJinvoiceID = cmd.ExecuteScalar();“ – qwe 2015-04-02 01:00:48

+0

重建您的数据库实现,因此它的东西类似于:invoiceID int NOT NULL并确保它设置为身份。 http://stackoverflow.com/questions/10991894/auto-increment-primary-key-in-sql-server-management-studio-2012)所以你可以轻松地做到这一点。 – vLr 2015-04-03 14:38:55

0

删除invoiceID我不是一个.NET的家伙,但通过google搜索,我认为你有没有提到你的插入查询标识字段:

using (var cmd = con.CreateCommand()) 
      { 
       cmd.CommandText = @"insert into Invoice(subtotal,tax,total) values (@subtotal,@tax,@total); 
// the rest 
      } 

PLZ采取看看这个简单的教程: w3schools

+0

是的,我曾尝试过,但我得到一个错误说:“不能将值NULL插入列'invoiceID',表;列不允许有空值。 INSERT失败。“ – qwe 2015-04-02 00:56:37

+0

错误发生在行”对象OBJinvoiceID = cmd.ExecuteScalar();“ – qwe 2015-04-02 01:01:28

0
insert into Invoice(subtotal,tax,total) values (@subtotal,@tax,@total); select SCOPE_IDENTITY() as invoiceID; 
+0

是的,我已经尝试过此,但我得到一个错误,说:”不能插入NULL值列'invoiceID',表;列不允许有空值。 INSERT失败。“ – qwe 2015-04-02 00:56:03

+0

错误发生在行”object OBJinvoiceID = cmd.ExecuteScalar();“ – qwe 2015-04-02 01:00:53