如何使用C#为sql server表添加列?

Mik*_*108 8 .net c# sql-server

如何使用C#为sql server表添加列?

例如,我想在C#代码中执行以下sql:

alter table [Product] add 
[ProductId] int default 0 NOT NULL
Run Code Online (Sandbox Code Playgroud)

Alf*_*ers 20

您应该使用命令:


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();
    }
}
Run Code Online (Sandbox Code Playgroud)