【问题标题】:Create a Sqlserver.Management.Smo column that is autoincremented创建一个自动递增的 Sqlserver.Management.Smo 列
【发布时间】:2013-10-24 21:05:30
【问题描述】:

我目前正在创建这样的数据库:

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);

但是现在假设我想添加一个自动增量列? 我注意到有一个IndexedColumn,但它没有说明自动增量。

有谁知道如何使用 Sqlserver.Management.Smo 完成自动增量?我对这个查询不感兴趣,我想用 Smo 来做。

【问题讨论】:

    标签: c# .net sql-server sql-server-2008 smo


    【解决方案1】:

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

    // 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/

    【讨论】:

      猜你喜欢
      • 2015-02-26
      • 2011-04-06
      • 2019-12-29
      • 2013-01-27
      • 2019-01-09
      • 2011-02-18
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多