如何使用c#将标识列添加到数据表中。我使用Sql紧凑型服务器。如何使用c将标识列添加到数据表中#
回答
你可以尝试这样的事情吗?
private void AddAutoIncrementColumn()
{
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.AutoIncrement = true;
column.AutoIncrementSeed = 1000;
column.AutoIncrementStep = 10;
// Add the column to a new DataTable.
DataTable table = new DataTable("table");
table.Columns.Add(column);
}
除非您只想让应用程序成为单一用户应用程序,否则您不会在DataTable(或前端)上自动增量。
如果您需要自动增量,只需在数据库中执行该操作,然后将从数据库生成的自动增量标识检索到您的前端。
见我的答案在这里,只是改变SqliteDataAdapter到SqlDataAdapter的,SqliteConnection到的SqlConnection等:anyway see why I get this "Concurrency Violation" in these few lines of code??? Concurrency violation: the UpdateCommand affected 0 of the expected 1 records
我正在阅读平面文件中的数据,并需要向DataTable添加行号 - 有时候这是有效的,并且不会让我的应用程序成为单个用户。 – MrTelly 2012-07-16 10:33:32
是啊,或者输出.....我生成的输出是由多个独立的读取组成的,需要以单个序列结束。这是DataTable的一个非常重要的本地使用......与“单用户”类似的隔离,但不完全相同。 – 2017-12-31 23:44:45
只是我的两分钱。自动增量处于Winform
应用程序非常有用(单独作为迈克尔·布埃诺说得很有道理),即:
DatagridView
被用于显示不具有“关键领域”的数据,同样可以用于枚举。
我不认为它使用自动增量上的数据表,如果您使用的插入和删除的数据表,因为数量不会被rearranget一个好主意,没有最终我将分享一个小想法,我们如何使用自动增量手册。
DataTable dt = new DataTable();
dt.Columns.Add("ID",typeof(int));
dt.Columns.Add("Produto Nome", typeof(string));
dt.Rows.Add(null, "A");
dt.Rows.Add(null, "B");
dt.Rows.Add(null, "C");
for(int i=0;i < dt.Rows.Count;i++)
{
dt.Rows[i]["ID"] = i + 1;
}
总是敲定时插入或删除,必须运行此循环
for(int i=0;i < dt.Rows.Count;i++)
{
dt.Rows[i]["ID"] = i + 1;
}
这真是令人困惑。并且改变'DataTable'不会改变数据库,我相信这是OPs问题的重要部分。 – andr 2013-01-18 00:51:24
DataTable table = new DataTable("table");
DataColumn dc= table.Columns.Add("id", typeof(int));
dc.AutoIncrement=true;
dc.AutoIncrementSeed = 1;
dc.AutoIncrementStep = 1;
// Add the new column name in DataTable
table.Columns.Add("name",typeof(string));
table.Rows.Add(null, "A");
table.Rows.Add(null, "B");
table.Rows.Add(null, "C");
如果已经填充了DataTable的。你可以使用下面的方法
void AddAndPopulateDataTableRowID(DataTable dt, string col, bool isGUID)
{
if(isGUID)
dt.Columns.Add(col, typeof(System.Guid));
else
dt.Columns.Add(col, typeof(System.Int32));
int rowid = 1;
foreach (DataRow dr in dt.Rows)
{
if (isGUID)
dr[col] = Guid.NewGuid();
else
dr[col] = rowid++;
}
}
- 1. Teradata:如何将标识列添加到现有表中?
- 2. 如何使用Subsonic将列添加到数据库表中?
- 3. 将标识添加到现有列中
- 4. 如何从数据库中使用c#获取单列数据(以及如何将其添加到列表中)
- 5. 如何将C#中的数据添加到动态添加的下拉列表
- 6. 使用表格适配器将列添加到数据集c#
- 7. 如何将光标数据(DbHandler.java)添加到列表视图(MainActicity.java)?
- 8. 将标识列添加到数据库中的所有表Sql server 2005
- 9. 将列添加到数据表中并添加数据
- 10. 将列添加到Hibernate数据表中
- 11. 使用Sql Server中的数据为现有列添加标识
- 12. C#如何将数据添加到没有标题的Excel中
- 13. 使用宏将标题添加到列数据中
- 14. C# - 将数据添加到列表内部列表
- 15. 将列表数据添加到列表(LINQ?)在C#
- 16. 如何使用Linq将列表集合添加到列表中?
- 17. 使用构造函数C将项添加到列表中#
- 18. 将标识列添加到始终更改的现有表中
- 19. 在C#中解析后将json数据添加到列表中
- 20. 如何将添加噪声添加到数字序列C#中?
- 21. 将数据添加到数组列表
- 22. C#将数据添加到阵列
- 23. 如何将数据集中的值添加到列表中?
- 24. 将元数据/标识符数据添加到CSV文件?
- 25. 如何使用C#将主文件添加到列表视图?
- 26. 如何使用ajax将html表值添加到数据库中
- 27. 如何使用Asp.net将批量表添加到数据库中
- 28. 如何使用RODBC将数据框添加到mysql表中
- 29. 如何将本地数据库表标识列数据移动到Azure SQL表
- 30. 使用VBA将数据添加到Excel中的Sharepoint 2013列表
当添加到一个现有的DataTable中的数据,它会自动填充现有的行中的新列? – Lunyx 2017-05-12 13:35:50