如何增加一个自动递增字段使用ADO.NET在C# [英] How to increment an auto-increment field with ADO.NET in C#

查看:448
本文介绍了如何增加一个自动递增字段使用ADO.NET在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个字段一个数据库表:

I have a database table with three fields:

产品(PRODID,名称,价格)

其中的 PRODID 的是自动递增字段。自动增量种子1,从0开始。

Where ProdId is a auto-increment field. The auto-increment is with seed 1, starting from 0.

我用在ASP.NET应用程序的C#在数据库上执行的操作。

I perform operation on the database by using C# in an ASP.NET application.

如果我使用LINQ插入一个新的记录我刚分配值名称和价格领域,当我提交更改的数据库负责递增PRODID。

If I use LINQ to INSERT a new record I just assign values to Name and Price fields and when I submit the changes the database is responsible to increment the ProdId.

我如何能做到这一点与标准ADO.NET?如果我写INSERT语句

How can I do it with the standard ADO.NET? If I write an INSERT statement

INSERT INTO Product (Name, Price) VALUES (@Name, @Price)

当我执行一个异常被抛出的命令;

when I execute the command an exception is thrown;

无法在PRODID插入NULL。

任何人有一个解决方案吗?谢谢

Anybody has a solution? Thanks

推荐答案

我会按以下顺序启动调试:

I would start debugging in this order:

  • 确保您的SQL你有自动递增

数据类型可以是数字金钱十进制浮动 BIGINT INT

  • 使用Studio的管理工具,手动运行插入查询

这样的:

DECLARE @Name nvarchar(100), @Price money

SET @Name = 'Bruno Alexandre';
SET @Price = 100.10;

INSERT INTO Product (Name, Price) VALUES (@Name, @Price)

INSERT INTO Product SELECT @Name, @Price

  • 确保你是指向你的ADO连接到正确的数据库。

    • Make SURE that you are pointing in your ADO Connection to the correct Database.

      如果使用EF,刷新你的表使用更新表向导在的.edmx 文件

      If using EF, refresh your table using the Update Table Wizard on the .edmx file

      这篇关于如何增加一个自动递增字段使用ADO.NET在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆