Linq to SQL-在InsertOnSubmit()之后如何查找IDENTITY列的值 [英] Linq to SQL - How to find the value of the IDENTITY column after InsertOnSubmit()

查看:91
本文介绍了Linq to SQL-在InsertOnSubmit()之后如何查找IDENTITY列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 LINQ to SQL 将简单数据插入到没有stored procedure的表中.该表具有一个主键ID"列,该列在SQL Server和我的DBML中均设置为IDENTITY列.

I am using LINQ to SQL to insert simple data into a table WITHOUT a stored procedure. The table has a Primary Key ID column, which is set as an IDENTITY column in both SQL Server and in my DBML.

首先,我用一行的数据调用InsertOnSubmit();,然后调用SubmitChanges();将该行提交给db.但是我认为必须有一种简单的方法来检索新插入行的行的IDENTITY列值.我不想向数据库提供IDENTITY列值,我希望它为我生成一个值.

First I call InsertOnSubmit(); with data for a single row, and then I call SubmitChanges(); to commit the row to the db. But I think there must be a simple way to then retrieve the row's IDENTITY column value of the newly inserted row. I don't wish to provide the IDENTITY column value to the db, I want it to generate one for me.

如何最好地处理?

推荐答案

这是一个简单的代码段

            Dim odb As New DataClassesDataContext
            Dim tst As New test
            tst.name = "abcd"
            odb.tests.InsertOnSubmit(tst)
            odb.SubmitChanges()
            Response.Write("id:" + tst.id.ToString)

因此,基本上,您在InsertOnSubmit中使用的对象将在数据库中创建记录后填充身份字段

so, basically the object you used in InsertOnSubmit will get the identity field populated after the record has been created in the database

这篇关于Linq to SQL-在InsertOnSubmit()之后如何查找IDENTITY列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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