usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
publicpartialclassslu1:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!IsPostBack)
{
if(execfile())
{
Response.Write("Success");
}
}
}
///<summary>
///创建连接起用进程建立数据库
///</summary>
///<returns></returns>
privateboolexecfile()
{
try
{
stringconnStr="datasource=127.0.0.1;userid=sa;password=sa;persistsecurityinfo=false;packetsize=4096";
ExecuteSql(connStr,"master","CREATEDATABASE"+"SqlTest");//调用ExecuteNonQuery()来创建数据库
System.Diagnostics.ProcesssqlProcess=newSystem.Diagnostics.Process();//创建一个进程
sqlProcess.StartInfo.FileName="osql.exe";//OSQL基于ODBC驱动连接服务器的一个实用工具(可查阅SQL帮助手册)
//stringstr=@"C:ProgramFilesMicrosoftSQLServerMSSQLData";
sqlProcess.StartInfo.Arguments="-Usa-Psa-dSqlTest-iC:ProgramFilesMicrosoftSQLServerMSSQLData";//获取启动程序时的参数
sqlProcess.StartInfo.WindowStyle=System.Diagnostics.ProcessWindowStyle.Hidden;//调用进程的窗口状态,隐藏为后台
sqlProcess.Start();
sqlProcess.WaitForExit();
sqlProcess.Close();
returntrue;
}
catch(Exceptionex)
{
throwex;
}
}
///<summary>
///创建数据库,调用ExecuteNonQuery()执行
///</summary>
///<paramname="conn"></param>
///<paramname="DatabaseName"></param>
///<paramname="Sql"></param>
privatevoidExecuteSql(stringconn,stringDatabaseName,stringSql)
{
System.Data.SqlClient.SqlConnectionmySqlConnection=newSystem.Data.SqlClient.SqlConnection(conn);
System.Data.SqlClient.SqlCommandCommand=newSystem.Data.SqlClient.SqlCommand(Sql,mySqlConnection);
Command.Connection.Open();
Command.Connection.ChangeDatabase(DatabaseName);
try
{
Command.ExecuteNonQuery();
}
finally
{
Command.Connection.Close();
}
}
}