当前位置: 编程技术>.net/c#/asp.net
C#自动创建数据库实现代码
来源: 互联网 发布时间:2014-10-13
本文导语: using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; publi...
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class slu1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (execfile())
{
Response.Write("Success");
}
}
}
///
/// 创建连接起用进程建立数据库
///
///
private bool execfile()
{
try
{
string connStr = "data source=127.0.0.1;user id=sa;password=sa;persist security info=false;packet size=4096";
ExecuteSql(connStr, "master", "CREATE DATABASE" + " SqlTest");//调用ExecuteNonQuery()来创建数据库
System.Diagnostics.Process sqlProcess = new System.Diagnostics.Process();//创建一个进程
sqlProcess.StartInfo.FileName = "osql.exe";//OSQL基于ODBC驱动连接服务器的一个实用工具(可查阅SQL帮助手册)
//string str = @"C:Program FilesMicrosoft SQL ServerMSSQLData";
sqlProcess.StartInfo.Arguments = " -U sa -P sa -d SqlTest -i C:\Program Files\Microsoft SQL Server\MSSQL\Data";//获取启动程序时的参数
sqlProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//调用进程的窗口状态,隐藏为后台
sqlProcess.Start();
sqlProcess.WaitForExit();
sqlProcess.Close();
return true;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 创建数据库,调用ExecuteNonQuery()执行
///
///
///
///
private void ExecuteSql(string conn, string DatabaseName, string Sql)
{
System.Data.SqlClient.SqlConnection mySqlConnection = new System.Data.SqlClient.SqlConnection(conn);
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql, mySqlConnection);
Command.Connection.Open();
Command.Connection.ChangeDatabase(DatabaseName);
try
{
Command.ExecuteNonQuery();
}
finally
{
Command.Connection.Close();
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class slu1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (execfile())
{
Response.Write("Success");
}
}
}
///
/// 创建连接起用进程建立数据库
///
///
private bool execfile()
{
try
{
string connStr = "data source=127.0.0.1;user id=sa;password=sa;persist security info=false;packet size=4096";
ExecuteSql(connStr, "master", "CREATE DATABASE" + " SqlTest");//调用ExecuteNonQuery()来创建数据库
System.Diagnostics.Process sqlProcess = new System.Diagnostics.Process();//创建一个进程
sqlProcess.StartInfo.FileName = "osql.exe";//OSQL基于ODBC驱动连接服务器的一个实用工具(可查阅SQL帮助手册)
//string str = @"C:Program FilesMicrosoft SQL ServerMSSQLData";
sqlProcess.StartInfo.Arguments = " -U sa -P sa -d SqlTest -i C:\Program Files\Microsoft SQL Server\MSSQL\Data";//获取启动程序时的参数
sqlProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;//调用进程的窗口状态,隐藏为后台
sqlProcess.Start();
sqlProcess.WaitForExit();
sqlProcess.Close();
return true;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 创建数据库,调用ExecuteNonQuery()执行
///
///
///
///
private void ExecuteSql(string conn, string DatabaseName, string Sql)
{
System.Data.SqlClient.SqlConnection mySqlConnection = new System.Data.SqlClient.SqlConnection(conn);
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql, mySqlConnection);
Command.Connection.Open();
Command.Connection.ChangeDatabase(DatabaseName);
try
{
Command.ExecuteNonQuery();
}
finally
{
Command.Connection.Close();
}
}
}