C # Automatically create DATABASE implementation code _c# tutorial

Source: Internet
Author: User
Tags microsoft sql server mssql create database
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>
Creating a connection to create a database with a process
</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 databases

System.Diagnostics.Process sqlprocess = new System.Diagnostics.Process ();//Create a process

SqlProcess.StartInfo.FileName = "Osql.exe";//osql a utility that is based on ODBC-driven connection to the server (consult 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";//Get Boot The parameters of the order
SqlProcess.StartInfo.WindowStyle = system.diagnostics.processwindowstyle.hidden;//The window state of the calling process, hidden as background
Sqlprocess.start ();
Sqlprocess.waitforexit ();
Sqlprocess.close ();
return true;
}
catch (Exception ex)
{
Throw ex;
}
}

<summary>
Create DATABASE, call ExecuteNonQuery () Execute
</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.