0

I want to add one column to an existing table in C#, if it doesn't already exist. I know I have to use an 'alter table' command.

But I am not able to fire that command in my C# code.
How can I do?

I'm using Visual Studio 2010 and Sql Server 2008.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • http://www.c-sharpcorner.com/UploadFile/mahesh/CreatingDBProgrammaticallyMCB11282005064852AM/CreatingDBProgrammaticallyMCB.aspx – A_Sk Mar 18 '15 at 07:23
  • 2
    possible duplicate of [C# Alter Table and Add a column programmatically ASP.Net & SQL Server](http://stackoverflow.com/questions/20719449/c-sharp-alter-table-and-add-a-column-programmatically-asp-net-sql-server) – Rajesh Mar 18 '15 at 07:23
  • Fixed some grammar and formatting, Removed irrelevant greetings and thanks – Phantômaxx Mar 18 '15 at 11:31

1 Answers1

0

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
Andriy M
  • 76,112
  • 17
  • 94
  • 154
Iraj
  • 1,492
  • 5
  • 18
  • 42