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

DBAccess.cs
  1. using System;
  2. using System.Xml;
  3. using System.Data;
  4. using System.IO;
  5. using System.Collections;
  6. using System.Data.SqlClient;
  7. using System.Diagnostics;
  8. namespace ExecuteSqlFile
  9. {
  10.     /// <summary>
  11.     /// DBAccess 的摘要说明。
  12.     /// </summary>
  13.     public class DBAccess
  14.     {
  15.         public DBAccess()
  16.         {
  17.         }
  18.         #region 属性
  19.         //server=TEST;uid=sa;pwd=1234567890;database=DB
  20.         private static string ConStr = "";
  21.         public static string ConString
  22.         {
  23.             get
  24.             {
  25.                  return ConStr;
  26.             }
  27.             set
  28.             {
  29.                 ConStr = value;
  30.             }
  31.         }
  32.         private static SqlConnection Con;
  33.         public static SqlConnection MyConnection
  34.         {
  35.             get
  36.             {
  37.                 if (Con == null)
  38.                 {
  39.                     Con = new SqlConnection(ConString);
  40.                 }
  41.                 return Con;
  42.             }
  43.         }
  44.         #endregion
  45.         /// <summary>
  46.         /// 执行Sql文件
  47.         /// </summary>
  48.         /// <param name="varFileName"></param>
  49.         /// <returns></returns>
  50.         public static bool ExecuteSqlFile(string varFileName)
  51.         {
  52.             if (!File.Exists(varFileName))
  53.             {
  54.                 return false;
  55.             }
  56.             StreamReader sr = File.OpenText(varFileName);
  57.             ArrayList alSql = new ArrayList();
  58.             string commandText = "";
  59.             string varLine = "";
  60.             while (sr.Peek() > -1)
  61.             {
  62.                 varLine = sr.ReadLine();
  63.                 if (varLine == "")
  64.                 {
  65.                     continue;
  66.                 }
  67.                 if (varLine != "GO")
  68.                 {
  69.                     commandText += varLine;
  70.                     commandText += " ";
  71.                 }
  72.                 else
  73.                 {
  74.                     alSql.Add(commandText);
  75.                     commandText = "";
  76.                 }
  77.             }
  78.             sr.Close();
  79.             try
  80.             {
  81.                 ExecuteCommand(alSql);
  82.             }
  83.             catch(Exception ex)
  84.             {
  85.                 //return false;
  86.                 throw ex;
  87.             }
  88.             return true;
  89.         }
  90.         private static void ExecuteCommand(ArrayList varSqlList)
  91.         {
  92.             MyConnection.Open();
  93.             SqlTransaction varTrans = MyConnection.BeginTransaction();
  94.             SqlCommand command = new SqlCommand();
  95.             command.Connection = MyConnection;
  96.             command.Transaction = varTrans;
  97.             try
  98.             {
  99.                 foreach (string varcommandText in varSqlList)
  100.                 {
  101.                     command.CommandText = varcommandText;
  102.                     command.ExecuteNonQuery();
  103.                 }
  104.                 varTrans.Commit();
  105.             }
  106.             catch (Exception ex)
  107.             {
  108.                 varTrans.Rollback();
  109.                 throw ex;
  110.             }
  111.             finally
  112.             {
  113.                 MyConnection.Close();
  114.             }
  115.         }
  116.         public static bool ExecOSQL(string sServer,string sUser,string sPwd,string sSqlFile)
  117.         {
  118.             try   
  119.             {
  120.                 Process   p   =   new   Process(); 
  121.                 p.StartInfo.FileName   =   "cmd.exe ";  
  122.                 p.StartInfo.UseShellExecute   =   false;  
  123.                 p.StartInfo.RedirectStandardInput   =   true;  
  124.                 p.StartInfo.RedirectStandardOutput   =   false;
  125.                 p.StartInfo.RedirectStandardError   =   true;  
  126.                 p.StartInfo.CreateNoWindow   =   true;   
  127.                 //string   Path   =   Application.StartupPath.ToString(); 
  128.                 string   Parameter   =   "osql.exe   -U " + sUser  + " -P "+ sPwd + " -S " +  sServer + " -i " + sSqlFile;
  129.                
  130.                 //MessageBox.Show(Parameter); 
  131.                 //this.Cursor   =   System.Windows.Forms.Cursors.WaitCursor; 
  132.                 p.Start();   
  133.                 p.StandardInput.WriteLine(Parameter);   
  134.                 p.StandardInput.WriteLine( "exit ");   
  135.                 p.StandardInput.WriteLine( "exit "); 
  136.                 p.WaitForExit();   
  137.                 p.Close(); 
  138.             }   
  139.             catch(Exception ex)   
  140.             {   
  141.                 throw ex;
  142.             }
  143.             return true;
  144.         }
  145.         //...
  146.     }
  147. }

 

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.         }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值