Automatically create a database in C #

Source: Internet
Author: User

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 ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.