在VS中通过代码创建数据库(控制台…
2015-04-19 22:58
225 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace CreateDataBaseByVS
{
class Program
{
static void Main(string[] args)
{
try
{
string conStr = "Data
Source=.;Initial Catalog=master;User id=sa;pwd=123;";
SqlConnection conn = new
SqlConnection(conStr);
conn.Open();
StringBuilder sb = new
StringBuilder();
sb.AppendLine("use
master");
sb.AppendLine("if exists
(select * from sysdatabases where name='q')");
sb.AppendLine("drop database
q");
sb.AppendLine("Create
Database q");
sb.AppendLine("on
primary");
sb.AppendLine("(");
sb.AppendLine("name='q',");
sb.AppendLine("FILENAME='D:\\q.mdf',");
sb.AppendLine("size=15mb,");
sb.AppendLine("maxsize=100mb,");
sb.AppendLine("filegrowth=15%");
sb.AppendLine(")");
sb.AppendLine("log
on");
sb.AppendLine("(");
sb.AppendLine("name='q.ldf',");
sb.AppendLine("FILENAME='D:\\q.ldf',");
sb.AppendLine("size=15mb");
sb.AppendLine(")");
SqlCommand command = new
SqlCommand(sb.ToString(), conn);
int it
=(int)command.ExecuteScalar();//执行命令,要有,只为了执行
//
Console.WriteLine("创建失败");
}
catch
(Exception)
{
Console.WriteLine("创建成功");
}
Console.Read();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace CreateDataBaseByVS
{
class Program
{
static void Main(string[] args)
{
try
{
string conStr = "Data
Source=.;Initial Catalog=master;User id=sa;pwd=123;";
SqlConnection conn = new
SqlConnection(conStr);
conn.Open();
StringBuilder sb = new
StringBuilder();
sb.AppendLine("use
master");
sb.AppendLine("if exists
(select * from sysdatabases where name='q')");
sb.AppendLine("drop database
q");
sb.AppendLine("Create
Database q");
sb.AppendLine("on
primary");
sb.AppendLine("(");
sb.AppendLine("name='q',");
sb.AppendLine("FILENAME='D:\\q.mdf',");
sb.AppendLine("size=15mb,");
sb.AppendLine("maxsize=100mb,");
sb.AppendLine("filegrowth=15%");
sb.AppendLine(")");
sb.AppendLine("log
on");
sb.AppendLine("(");
sb.AppendLine("name='q.ldf',");
sb.AppendLine("FILENAME='D:\\q.ldf',");
sb.AppendLine("size=15mb");
sb.AppendLine(")");
SqlCommand command = new
SqlCommand(sb.ToString(), conn);
int it
=(int)command.ExecuteScalar();//执行命令,要有,只为了执行
//
Console.WriteLine("创建失败");
}
catch
(Exception)
{
Console.WriteLine("创建成功");
}
Console.Read();
}
}
}
相关文章推荐
- 通过写代码,attach程序中创建的其他进程,将其加入VS的Debugger,以方便调试
- 通过beego快速创建一个Restful风格API项目及API文档自动化 本文演示如何快速(一分钟内,不写一行代码)的根据数据库及表创建一个Restful风格的API项目,及提供便于在线测试API的界
- 如何在代码中通过命令行创建SQL SERVER 数据库
- 获取本机MAC地址,创建GUID,硬盘序列号,vs2013代码通过验证
- YbSoftwareFactory 代码生成插件【十四】:通过 DynamicLinq 简单实现 N-Tier 部署下的服务端数据库通用分页
- easyui-datagrid通过action从数据库获取数据的关键代码
- php 通过 create user 和grant 命令无法创建数据库用户和授权的解决办法
- 使用VS的动态连接库创建通用数据库连接对话框
- c#通过纯代码创建桌面快捷方式、创建程序菜单项、将网页添加到收藏夹
- 26-UI基础通过代码创建九宫格
- s60 2nd通过代码创建cmnet接入点的方法
- Android的app为什么我通过adb删除了数据库之后,为什么创建数据库没反应
- 通过Javascript创建一个选择文件的对话框代码
- asp.net通过js实现Cookie创建以及清除Cookie数组的代码
- 977dh用vbs通过wsh创建桌面快捷方式,快速启动,修改ie主页的恶意代码 ...
- query ajax json 通过后台代码(查询数据库)绑定html控件select 下拉框实例
- 通过代码创建Java项目
- PHP中创建空文件的代码[file_put_contents vs touch]
- sql server 数据库的创建(代码)