0

I am currently creating a database like this :

var database = new server(serverconnection blah blah);
var table = new Table(database, "tablename");

var name = new Column(tab, "Name", DataType.NVarChar(255));
tab.Columns.Add(name);

var something = new Column(tab, "Something", DataType.NVarChar(255));
tab.Columns.Add(something);

But now lets say I wanted to add an autoincrementing column? I noticed there was an IndexedColumn but it doesn't say anything about autoincrementing.

Does anyone know how to accomplish autoincrementing with Sqlserver.Management.Smo? I'm not interested in a query for this, I want to do it using Smo.

4

1 回答 1

3

您正在寻找“身份列”。像这样的东西应该工作:

// Add ‘ID’ column which is the primary key
Column idColumn = new Column(table, "ID");
idColumn.DataType = DataType.Int;
idColumn.Identity = true;
idColumn.IdentitySeed = 1;
idColumn.IdentityIncrement = 1;

// Create a primary key index
Index index = new Index(table, string.Format("PK_{0}", table.Name));
index.IndexKeyType = IndexKeyType.DriPrimaryKey;
index.IndexedColumns.Add(new IndexedColumn(index, "ID"));
table.Indexes.Add(index);


// Add colums to table
table.Columns.Add(idColumn);

此博客/文章中的示例代码:http: //www.christophdebaene.com/blog/2007/12/31/programmatically-creating-tables-in-sql-server-using-net-and-smo/

于 2013-10-25T00:02:06.180 回答