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

前端 未结 2 1764
说谎
说谎 2020-12-10 06:44

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 
[Pr         


        
相关标签:
2条回答
  • 2020-12-10 07:13
    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

    0 讨论(0)
  • 2020-12-10 07:16

    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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题