【问题标题】:How to add identity column to datatable using c#如何使用 c# 将标识列添加到数据表
【发布时间】:2010-05-15 06:33:23
【问题描述】:

如何使用 c# 将标识列添加到数据表。我正在使用 Sql 紧凑型服务器。

【问题讨论】:

    标签: c#


    【解决方案1】:

    你可以试试这样的东西吗?

    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 时,这会自动填充现有行中的新列吗?
    • 一行版本:table.Columns.Add(new DataColumn("Id", typeof(int)) { AutoIncrement = true, AutoIncrementSeed = 1, AutoIncrementStep = 1 });
    【解决方案2】:
     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");
    

    【讨论】:

      【解决方案3】:

      如果 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++;
              }
      
          }
      

      【讨论】:

        【解决方案4】:

        除非您想让您的应用程序仅成为单用户应用程序,否则您不会对 DataTable(或前端)进行自动增量。

        如果您需要自动增量,只需在数据库中执行,然后将数据库生成的自动增量 id 检索到您的前端。

        在这里查看我的答案,只需将 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 添加行号 - 有时这是有效的,但不会让我的应用成为单一用户。
        • 是的,或者用于输出.....我正在生成由多个独立读取组成的输出,这些读取需要以单个序列结束。这是 DataTable 的一个非常重要的本地使用......类似于“单用户”的隔离,但根本不是一回事。
        【解决方案5】:

        只有我的两分钱。自动增量在 Winform 应用程序中很有用(正如 Michael Buen 所说的那样独立),即:

        DatagridView用于显示没有“关键字段”的数据,同样可以用于枚举。

        【讨论】:

          【解决方案6】:

          如果您对数据表使用插入和删除,我认为在数据表上使用自动增量不是一个好主意,因为数字不会重新排列,没有最终我将分享一个小想法,我们如何使用自动增量手册。

                   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 问题的重要部分。
          猜你喜欢
          • 2011-02-03
          • 2022-01-18
          • 2021-11-30
          • 1970-01-01
          • 2020-04-07
          • 2021-02-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多