C#自动创建数据库实现代码
程序员文章站
2022-06-21 08:20:26
using system; using system.data; using system.configuration...
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");
}
}
}
/// <summary>
/// 创建连接起用进程建立数据库
/// </summary>
/// <returns></returns>
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 files\microsoft sql server\mssql\data";
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;
}
}
/// <summary>
/// 创建数据库,调用executenonquery()执行
/// </summary>
/// <param name="conn"></param>
/// <param name="databasename"></param>
/// <param name="sql"></param>
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");
}
}
}
/// <summary>
/// 创建连接起用进程建立数据库
/// </summary>
/// <returns></returns>
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 files\microsoft sql server\mssql\data";
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;
}
}
/// <summary>
/// 创建数据库,调用executenonquery()执行
/// </summary>
/// <param name="conn"></param>
/// <param name="databasename"></param>
/// <param name="sql"></param>
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();
}
}
}
上一篇: Ubuntu 16.04安装Apache Tomcat的方法
下一篇: 蛋的营养价值,你知道多少呢