当前位置: 代码迷 >> ASP.NET >> SQL的自动增长列用C#如何实现
  详细解决方案

SQL的自动增长列用C#如何实现

热度:9260   发布时间:2013-02-25 00:00:00.0
SQL的自动增长列用C#怎么实现?
SQL里面有一张表字段 int ID为主键。
然后我想从C#里给这个主键弄成自动增长列,而不是在SQL里面改。
通过代码来实现.请大家指点一下。
先谢谢了。

------解决方案--------------------------------------------------------
探讨
ds.Tables[0].Columns["hjhID"].AutoIncrementSeed = 10000;
ds.Tables[0].Columns["hjhID"].AutoIncrementStep = 1;

------解决方案--------------------------------------------------------
//设置自增长
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.AutoIncrement = true;
column.AutoIncrementSeed = 1000;
column.AutoIncrementStep = 1;

// Add the column to a new DataTable.
DataTable table = new DataTable("table");
table.Columns.Add(column);
//插入记录:
DataRow row = table.NewRow();
row["列1"] = "值1";
row["列2"] = "值2";
...
table.Rows.Add(row);
...
  相关解决方案