c sharp automatically creates the database implementation code

  • 2020-05-05 11:48:20
  • OfStack

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 to start the process to create the 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 database  

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

                  StartInfo   =   "osql exe"; //OSQL is a utility based on ODBC that drives connections to servers (see the SQL help manual)  
                        //string   str   =   @"C:\Program   Files\Microsoft   SQL   Server\MSSQL\Data";  

                    =   "  U   sa  -P   sa  -d   SqlTest   C:\\Program   Files\\ \ // gets the parameter  
when the program is started                         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 >  
        /// /   creates the database and invokes ExecuteNonQuery() to 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();  
                }  
        }  
}

Related articles: