Joh*_*ith 5 c# datatable auto-increment
我有一个数据表,列A,B,C.我已将列A的"is identity"属性设置为true,但我现在无法向表中添加任何行.
我正在尝试的代码是这样的:
dsA.dtA row = dsA.dtA.NewdtARow();
row.B = 1;
row.C = 2;
dsA.dtA.Rows.Add(row);
Run Code Online (Sandbox Code Playgroud)
我得到NoNullAllowedException,但我不明白为什么.A列也是PK.如果我试图设置row.A = 5(或任何类似的),当我尝试更新数据表时说"当identity_insert设置为off时无法为表中的标识列插入显式值"时,我会收到错误
我该如何解决这个问题?这令人沮丧.
试试这个.
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.AutoIncrement = true;
column.AutoIncrementSeed = 1;
column.AutoIncrementStep = 1;
// Add the column to a new DataTable.
DataTable table = new DataTable("table");
table.Columns.Add(column);
DataRow oRow = table.NewRow();
table.Rows.Add(oRow);
Run Code Online (Sandbox Code Playgroud)