插入新记录时如何获取主键值? [英] How can i get primary key value when i insert a new record?

查看:24
本文介绍了插入新记录时如何获取主键值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 LINQ-to-SQL 类.它有一个方法 object.InsertOnSubmit() .但它返回 void 所以我可以在插入记录后获取主键值.

I am using LINQ-to-SQL class. It has a method object.InsertOnSubmit() .But it returns void so can i get primary key values after inserting a record.

我想要最近插入记录的主键值.

I want the primary key value of recently inserted record.

推荐答案

是(假设它是一个标识字段).

yes (Assuming it is an identity field).

单独调用 InsertOnSubmit 不会将插入发送到数据库.您需要调用 SubmitChanges 以便将当前数据上下文实例中的任何更改发送到数据库.

Calling InsertOnSubmit alone doesn't send the insert to the db. You need to call SubmitChanges in order for any changes in the current datacontext instance to be sent to the db.

调用 SubmitChanges 后,您可以访问调用 InsertOnSubmit 时使用的实例中的值.Linq to sql 将在执行插入时为您填充这些值(发生在 SubmitChanges 期间)

After you have called SubmitChanges, you can access the values from the instance you used when calling InsertOnSubmit. Linq to sql will populate those values for you when doing the insert (which occurs during SubmitChanges)

检查这个例子:http://msdn.microsoft.com/en-us/vbasic/bb737920.aspx#dynid

这篇关于插入新记录时如何获取主键值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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