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

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

问题描述

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

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不会将插入内容发送到db.您需要调用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/zh-cn/vbasic/bb737920.aspx#dynid

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

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