简体   繁体   English

拉动插入自动增量编号

[英]Pull on insert the auto increment id

I'm using a SQL Server ( .MDF ) database with C# and I want pull on insert the auto increment id. 我正在使用带有C#的SQL Server( .MDF )数据库,并且希望插入自动增量ID。

Currently I have a table with 3 cells: 目前,我有一个包含3个单元格的表:

  1. Id || ID || int INT
  2. StartDate || 开始日期|| datetime 约会时间
  3. GameMode || GameMode || nchar(10) 的nchar(10)

On code level I'm not inserting the ID cell, the setup of the database takes care of it: 在代码级别上,我没有插入ID单元,数据库的设置可以解决这个问题:

db.TblGames.InsertOnSubmit(new TblGame { StartDate = DateTime.Now, GameMode = gm.ToString() });
db.SubmitChanges();

Any ideas? 有任何想法吗?

I take it you're using LINQ to SQL. 我认为您正在使用LINQ to SQL。 The auto-generated id should be in the TblGame instance after .SubmitChanges() returns. 自动生成的ID应该在.SubmitChanges()返回之后的TblGame实例中。 Keep the TblGame instance to retrieve it, like this: 保留TblGame实例来检索它,如下所示:

var game = new TblGame { StartDate = DateTime.Now, GameMode = gm.ToString() };
db.TblGames.InsertOnSubmit(game);
db.SubmitChanges();
var id = game.Id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM