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

更多相关文章

  1. vs2012利用MFC开发基于对话框的小软件指南(连接Mysql数据库)
  2. Sql2005中,恢复数据库时,旁边显示“restricted user”,怎么办?
  3. PHP基础教程十四之使用MySqli操作数据库
  4. Nutz框架学习之一连接sqlserver数据库进行登录操作
  5. Java向MySQL数据库插入时间类型Date数据时需要注意的问题
  6. mysql数据库笔记
  7. 如何将表(及其行,列和单元格)保存在SQL数据库的单个字段中
  8. 第15天(就业班) 课程回顾、mysql安装、管理数据库、管理表、增删
  9. Mysql数据库学习笔记(一)

随机推荐

  1. android 导出签名APK--混淆文件proguard.
  2. Android Contacts的使用(三)
  3. android traceview and dmtracedump使用
  4. 使用android访问SQLServer数据库
  5. 插件入门,Android(安卓)Studio 生成 Butte
  6. Android浏览器如何打开本地html文件
  7. 设置行距
  8. Android mvvm使用实战
  9. Android -- 生命周期
  10. [Android]Android数据的四种存储方式