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>
/// Create a connection START process to create a database
/// </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"); // call executenonquery () to create a database
System. Diagnostics. Process sqlprocess = new system. Diagnostics. Process (); // create a process
Sqlprocess. startinfo. filename = "osql.exe"; // osql is a practical tool for connecting to the server based on the ODBC driver (see the SQL help manual)
// 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 "; // obtain the parameters of the Startup Program
Sqlprocess. startinfo. windowstyle = system. Diagnostics. processwindowstyle. Hidden; // The Window status of the calling process, which is hidden in the background.
Sqlprocess. Start ();
Sqlprocess. waitforexit ();
Sqlprocess. Close ();
Return true;
}
Catch (exception ex)
{
Throw ex;
}
}
/// <Summary>
/// Create a database and call executenonquery () for execution
/// </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 ();
}
}
}