如何使用C#来添加一列的SQL Server表? [英] How to use C# to add a column for a table of sql server?

查看:112
本文介绍了如何使用C#来添加一列的SQL Server表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C#来添加一列的SQL Server表?

例如,我想执行的C#code中的SQL语句:

  ALTER TABLE [产品]加
的[ProductID]整数默认0 NOT NULL
 

解决方案

您应该使用命令:

 
使用(的DbConnection连接=新的SqlConnection(你的连接字符串)){
    connection.Open();
    使用(的DbCommand命令=新的SqlCommand(ALTER TABLE [产品]添加的[ProductID]整数默认0 NOT NULL)){
        command.Connection =连接;
        command.ExecuteNonQuery();
    }
}
 

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

解决方案

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();
    }
}

这篇关于如何使用C#来添加一列的SQL Server表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆