如何用c#创建SQL Server 2012数据库?
16lz
2021-01-22
I'm creating a system using SQL Server 2012 for database, but when this system is starting first time in a computer, I want the system create the database and tables for me no create this manual.
我正在为数据库创建一个使用SQL Server 2012的系统,但是当这个系统在计算机中首次启动时,我希望系统为我创建数据库和表,而不是创建本手册。
How to I create database with C#? And how to I set connect string? because normally I set database name in the connect string, but now, I need create database before
如何使用c#创建数据库?如何设置连接字符串?因为通常我在connect字符串中设置数据库名,但是现在我需要以前创建数据库
1 个解决方案
#1
6
You can use SQL script, leave database name empty. Example code here :
您可以使用SQL脚本,保持数据库名称为空。示例代码:
static void Main(string[] args)
{
using (var conn = new SqlConnection("data source=DBServer; uid=UserName; pwd=Password;"))
{
using (var cmd = new SqlCommand())
{
conn.Open();
cmd.Connection = conn;
cmd.CommandText = "Create Database NewDB;";
cmd.ExecuteNonQuery();
cmd.CommandText = "Use NewDB;CREATE TABLE dbo.Table1 (ID int, Data nvarchar(128));";
cmd.ExecuteNonQuery();
}
}
}
更多相关文章
- vs2012利用MFC开发基于对话框的小软件指南(连接Mysql数据库)
- Sql2005中,恢复数据库时,旁边显示“restricted user”,怎么办?
- PHP基础教程十四之使用MySqli操作数据库
- Nutz框架学习之一连接sqlserver数据库进行登录操作
- Java向MySQL数据库插入时间类型Date数据时需要注意的问题
- mysql数据库笔记
- 如何将表(及其行,列和单元格)保存在SQL数据库的单个字段中
- 第15天(就业班) 课程回顾、mysql安装、管理数据库、管理表、增删
- Mysql数据库学习笔记(一)