【问题标题】:How to alter table from C# code如何从 C# 代码更改表
【发布时间】:2015-03-18 07:20:26
【问题描述】:

我想在 C# 中的现有表中添加一列,如果它不存在的话。我知道我必须使用“更改表”命令。

但我无法在我的 C# 代码中触发该命令。
我该怎么办?

我正在使用 Visual Studio 2010 和 Sql Server 2008。

【问题讨论】:

标签: c# asp.net .net sql-server database


【解决方案1】:

这个:

Use [DatabaseName]
Go
if Not exists( Select * from sys.columns As clm
                where clm.object_id = OBJECT_ID(N'[TableName]')
                And clm.name = N'[ColumnName]'
              )
Begin

    Alter Table TableName
    Add ColumnName DataTypeName

End

【讨论】:

  • thnx user3540365、Rajesh 和 raj。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多