VS2005/VS2008数据集设计器,将一行插入具有自动生成的guid列的表中
我有一个使用 VS2005/VS2008 数据集设计器创建的强类型数据表。
该表有一个主键列,它是一个 guid,由 SQL Server 填充。 问题是当我想向我的 DataTable 添加一行(或多行),然后调用 DataAdapter.Update 方法(传入DataTable)。 当 DataAdapter.Update 被调用时,我收到一个 SQL 异常,说我无法将 NULL 插入主键列。
如何告诉设计者这是一个自动生成的列,并且我不想为新行提供值? 我只想要 SQL 生成的值。
我在这里遗漏了一些东西,还是这是数据集设计器的限制?
我知道如何使用 LINQ to SQL 来实现这一点,但不幸的是,我没有它可以用于这个项目。
I have a strongly typed DataTable created with the VS2005/VS2008 DataSet designer.
The table has a Primary Key column that is a guid, which gets populated by SQL server. The problem is when I want add a row (or multiple rows) to my DataTable and then call the DataAdapter.Update method (passing in the DataTable). When DataAdapter.Update is called I get a SQL exception saying that I cannot insert NULL into the primary key column.
How do I tell the designer that this is an autogenerated column and I do not want to provide a value for new rows? I just want the value generated by SQL.
Am I missing something here, or is this a limitation of the DataSet designer?
I know how achieve this using LINQ to SQL, but unfortunatley I do not have it at my disposal for this project.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能是其中之一:
对于前两个选项,如果您希望设计器能够方便地使您的结构与数据库保持同步,那么您可以删除该列或以编程方式允许 null,也许就在解释原因的“// HACK:”注释旁边。
Possibly one of these:
For the first two options, if you want the convenience of letting the designer keep your structure in sync with your database, then you could remove the column or allow null programmatically, perhaps right next to a "// HACK: " comment explaining why.
您可能在 SQL Server 表定义上使用 DEFAULT NEWID(),因此问题可能是数据集设计器没有将此列视为自动生成的。
也许在应用程序代码中生成 guid 可能是一个解决方案? 如果没有,那么您可以在数据集中设置默认值,但您可能还必须更改 DataAdapter 插入/更新语句,以便该默认值不会插入到 Sql Server 表中。
可能还有其他一些我不知道的解决方案......
You're problably using DEFAULT NEWID() on your SQL Server table definition so the problem may be that the Dataset designer doesn't see this column as auto-generated.
Maybe generating guid in your application code could be a solution? If not, then you could set default value in you Dataset but then you're probably have to also change DataAdapter Insert/Update statements so that this default value doesn't get inserted into Sql Server table.
There could also be some other solution that I'm not aware of...