Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use C# to add a column for a table of sql server?

How to use C# to add a column for a table of sql server?

For example, I want to execute the following sql in C# code:

alter table [Product] add 
[ProductId] int default 0 NOT NULL
like image 242
Mike108 Avatar asked Aug 29 '09 15:08

Mike108


2 Answers

You should use a Command:


using (DbConnection connection = new SqlConnection("Your connection string")) {
    connection.Open();
    using (DbCommand command = new SqlCommand("alter table [Product] add [ProductId] int default 0 NOT NULL")) {
        command.Connection = connection;
        command.ExecuteNonQuery();
    }
}
like image 95
Alfred Myers Avatar answered Oct 20 '22 02:10

Alfred Myers


SqlCommand cmd2 = new SqlCommand();
// create columns for healed
cmd2 = new SqlCommand("ALTER TABLE TotalHeals ADD "+Healee+" INT", openCon);
cmd2.ExecuteNonQuery();

Funny how SqlCommand is different then DBCommand

like image 25
Jeremiah Stillings Avatar answered Oct 20 '22 04:10

Jeremiah Stillings