简体   繁体   English

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

[英]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? 如何使用C#为sql server表添加列?

For example, I want to execute the following sql in C# code: 例如,我想在C#代码中执行以下sql:

alter table [Product] add 
[ProductId] int default 0 NOT NULL

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();
    }
}
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 有趣的是SqlCommand与DBCommand有什么不同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用C#将列添加到SQL表 - Add column to sql table with c# C#Alter Table并以编程方式添加一列ASP.Net和SQL Server - C# Alter Table and Add a column programmatically ASP.Net & SQL Server 我可以在SQL Server和C#数据表的列名中使用特殊字符吗 - Can I use special characters in column name in SQL Server and C# data table 使用c#将MS Access表数据添加到SQL Server表 - Add MS Access table data to SQL Server table with c# 如何使用C#向SQL Server添加列和描述以及列类型 - How to add column and description and column type to sql server using c# 如何使用备份.sql文件使用C#控制台应用程序将数据库添加到sql server? - How to use backup .sql file to add databse to sql server using C# console application? 如何在windows forms(C#)中动态添加combobox并将其绑定到sql数据库表的一列 - how to dynamically add combobox in windows forms(C#) and bound it to a column of a table in sql database 如何通过VB中的C#编码将数据添加到SQL Server中的时间戳列 - how to add data to timestamp column in sql server through c# coding in vb 如何在C#中知道SQL Server中表的列是否为自动数字? - How can I know in C# if a column of a table in SQL Server is autonumeric? 如何检索SQL Server表的列值并将其存储在label.C#ASP.Net中的文本 - How to retrieve column value of sql server table and store it in label.Text of c# ASP.Net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM