【问题标题】:How Can I Insert New Column Into A Database Table Using SqlDataAdapter and DataTable?如何使用 SqlDataAdapter 和 DataTable 将新列插入数据库表?
【发布时间】:2011-02-06 11:43:55
【问题描述】:

在我的 .NET 程序中,我想通过插入新列来修改数据库表的结构。但我不想通过 ADO.NET 在数据库上运行“ALTER TABLE .....”命令来做到这一点。我想通过在我的数据表中添加新列来做到这一点。因此,当我更新关联的 tableAdapter 时,物理表结构将发生变化。

我期望的代码是这样的:

        SqlCommand cmd = new SqlCommand("SELECT * FROM EMPLOYEE", conn);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adapter.Fill(dt);
        dt.Columns.Add(new DataColumn("BirthDate"));
        adapter.Update(dt);

但它没有。

如果有可能,我该怎么做?

【问题讨论】:

    标签: .net ado.net datatable sqldataadapter


    【解决方案1】:

    你不能这样做。

    .NET 数据表只是磁盘上数据库结构的一种表示形式 - ADO.NET 数据表代码中没有支持生成更新底层的功能更改 DataTable 的结构时使用表。

    您将不得不使用ALTER TABLE ..... SQL 语句来实现您正在寻找的东西 - 恐怕没有办法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-21
      • 2014-11-18
      • 2013-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多