佈署程式時執行SQL文件來創建DB及相關物件

DBAccess.cs
using System; using System.Xml; using System.Data; using System.IO; using System.Collections; using System.Data.SqlClient; using System.Diagnostics; namespace ExecuteSqlFile {     /// <summary>     /// DBAccess 的摘要說明。     /// </summary>     public class DBAccess     {         public DBAccess()         {         }         #region 屬性         //server=TEST;uid=sa;pwd=1234567890;database=DB         private static string ConStr = "";         public static string ConString         {             get             {                  return ConStr;             }             set             {                 ConStr = value;             }         }         private static SqlConnection Con;         public static SqlConnection MyConnection         {             get             {                 if (Con == null)                 {                     Con = new SqlConnection(ConString);                 }                 return Con;             }         }         #endregion         /// <summary>         /// 執行Sql文件         /// </summary>         /// <param name="varFileName"></param>         /// <returns></returns>         public static bool ExecuteSqlFile(string varFileName)         {             if (!File.Exists(varFileName))             {                 return false;             }             StreamReader sr = File.OpenText(varFileName);             ArrayList alSql = new ArrayList();             string commandText = "";             string varLine = "";             while (sr.Peek() > -1)             {                 varLine = sr.ReadLine();                 if (varLine == "")                 {                     continue;                 }                 if (varLine != "GO")                 {                     commandText += varLine;                     commandText += " ";                 }                 else                 {                     alSql.Add(commandText);                     commandText = "";                 }             }             sr.Close();             try             {                 ExecuteCommand(alSql);             }             catch(Exception ex)             {                 //return false;                 throw ex;             }             return true;         }         private static void ExecuteCommand(ArrayList varSqlList)         {             MyConnection.Open();             SqlTransaction varTrans = MyConnection.BeginTransaction();             SqlCommand command = new SqlCommand();             command.Connection = MyConnection;             command.Transaction = varTrans;             try             {                 foreach (string varcommandText in varSqlList)                 {                     command.CommandText = varcommandText;                     command.ExecuteNonQuery();                 }                 varTrans.Commit();             }             catch (Exception ex)             {                 varTrans.Rollback();                 throw ex;             }             finally             {                 MyConnection.Close();             }         }         public static bool ExecOSQL(string sServer,string sUser,string sPwd,string sSqlFile)         {             try                {                 Process   p   =   new   Process();                  p.StartInfo.FileName   =   "cmd.exe ";                   p.StartInfo.UseShellExecute   =   false;                   p.StartInfo.RedirectStandardInput   =   true;                   p.StartInfo.RedirectStandardOutput   =   false;                 p.StartInfo.RedirectStandardError   =   true;                   p.StartInfo.CreateNoWindow   =   true;                    //string   Path   =   Application.StartupPath.ToString();                  string   Parameter   =   "osql.exe   -U " + sUser  + " -P "+ sPwd + " -S " +  sServer + " -i " + sSqlFile;                                 //MessageBox.Show(Parameter);                  //this.Cursor   =   System.Windows.Forms.Cursors.WaitCursor;                  p.Start();                    p.StandardInput.WriteLine(Parameter);                    p.StandardInput.WriteLine( "exit ");                    p.StandardInput.WriteLine( "exit ");                  p.WaitForExit();                    p.Close();              }                catch(Exception ex)                {                    throw ex;             }             return true;         }         //...     } }

 

Call it

 

  1. protected void btnExecSQL_Click(object sender, EventArgs e)
  2.         {
  3.             try
  4.             {
  5.                 ExecuteSqlFile.DBAccess.ConString = "server=TEST;uid=sa;pwd=1234567890;database=master";
  6.                 if(ExecuteSqlFile.DBAccess.ExecOSQL("TEST","sa","1234567890",Server.MapPath("CreaDB.sql")))
  7.                 {
  8.                     lbInfo.Text += "Create DB OK!";
  9.                     //if (ExecuteSqlFile.DBAccess.ExecOSQL("TEST", "sa", "1234567890", Server.MapPath("iTest.sql")))
  10.                     if (ExecuteSqlFile.DBAccess.ExecuteSqlFile(Server.MapPath("iTest.sql")))
  11.                     {
  12.                         lbInfo.Text += "Create Table OK!";
  13.                     }
  14.                     else
  15.                     {
  16.                         lbInfo.Text += "Create Table Err!";
  17.                     }
  18.                 }
  19.                 else
  20.                 {
  21.                     lbInfo.Text += "Create DB Err!";
  22.                 }
  23.             }
  24.             catch(Exception ex)
  25.             {
  26.                 lbInfo.Text += ex.Message;
  27.             }
  28.         }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章