简体   繁体   English

如何从C#代码更改表

[英]How to alter table from C# code

I want to add one column to an existing table in C#, if it doesn't already exist. 我想向C#中的现有表添加一列(如果尚不存在)。 I know I have to use an 'alter table' command. 我知道我必须使用“更改表”命令。

But I am not able to fire that command in my C# code. 但是我无法在C#代码中触发该命令。
How can I do? 我能怎么做?

I'm using Visual Studio 2010 and Sql Server 2008. 我正在使用Visual Studio 2010和Sql Server 2008。

this : 这个 :

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM