如何使用C#为SQL Server表添加列?
例如,我想在C#代码中执行以下SQL:
alter table [Product] add
[ProductId] int default 0 NOT NULL
如何使用C#为SQL Server表添加列?
例如,我想在C#代码中执行以下SQL:
alter table [Product] add
[ProductId] int default 0 NOT NULL
你应该使用一个命令:
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();
}
}
SqlCommand cmd2 = new SqlCommand();
// create columns for healed
cmd2 = new SqlCommand("ALTER TABLE TotalHeals ADD "+Healee+" INT", openCon);
cmd2.ExecuteNonQuery();
SqlClient
命名空间中支持 DDL。 - Alex Kudryashev