2017-05-06 68 views
2

我從C#代碼運行SQL腳本,我的問題是代碼需要很長時間才能完成執行,它有許多表在腳本中創建(至少30表),所以我的代碼正在工作,但它有性能問題。 這是代碼從ASP.Net運行SQL腳本C#應用程序性能問題

public static bool executeSqlScript(string scriptPath,string serverName,string databaseName) 
    { 
     try 
     { 

      SqlConnection myConn = new SqlConnection("Server=.;Integrated security=SSPI;database=master"); 
      Server server = new Server(new ServerConnection(myConn)); 
      string CreateCommand = "CREATE DATABASE " + databaseName + ""; 
      string appendText; 
      //delete first line from script(Oldscript) 
      var lines = File.ReadAllLines(scriptPath).Skip(1); 
      File.WriteAllLines(scriptPath, lines); 
      using (StreamReader sreader = new StreamReader(scriptPath)) 
      { 
       appendText = sreader.ReadToEnd(); 
      } 
      File.Delete(scriptPath); 
      using (StreamWriter swriter = new StreamWriter(scriptPath, false)) 
      { 
       appendText = "USE [" + databaseName + "]" + Environment.NewLine + appendText; 
       swriter.Write(appendText); 
       swriter.Close(); 
      } 
      string readtext = File.ReadAllText(scriptPath); 
      SqlCommand myCommand = new SqlCommand(CreateCommand, myConn); 
      myConn.Open(); 
      myCommand.ExecuteNonQuery(); 
      server.ConnectionContext.ExecuteNonQuery(readtext); 

      return true; 
     } 
     catch (Exception e) 
     { 
     throw e; 
     return false; 
     } 
    } 
+1

請將代碼粘貼到您的問題主體中。 –

+0

您需要多久創建一次這些表格?是否可以並行而不是串行地執行某些操作? –

+0

你是什麼意思串行? –

回答

0

的問題是在圍棋關鍵字在腳本中,我發現這個解決方案

http://www.codingdefined.com/2014/07/run-sql-script-file-in-c.html

(如果你想執行SQL生成的腳本文件,你必須刪除所有「GO」。爲此你必須使用下面的代碼...)

他re是我的代碼:

 string sqlConnectionString = "Data Source=.;Initial Catalog=master;Integrated Security=True"; 
     FileInfo file = new FileInfo(@"D:\Script.sql"); 
     string script = file.OpenText().ReadToEnd(); 
     SqlConnection conn = new SqlConnection(sqlConnectionString); 
     conn.Open(); 
     script = script.Replace("GO", ""); 
     SqlCommand cmd = new SqlCommand(script, conn); 
     cmd.ExecuteNonQuery(); 
     cmd.Dispose(); 
     conn.Close(); 
1

我的建議是這個最遷移到SQL Server,一旦新的數據庫和用戶的設置,調用存儲過程來讀取必要的DDL的SQL文件中它。

我最後的項目是託管託管公司,我們的CMS使用了150多個數據庫對象。我們使用了一個「控制」數據庫,我們傳遞了新的數據庫信息,並且通常會在不到一分鐘內完成新的表格,函數和過程。

+0

請給我一個這種方法的例子嗎? –

+0

不能給你具體說明,但[Sql Server Central](https://www.sqlservercentral.com/Forums/Topic452984-146-1.aspx)有一個論壇討論可以指導你 –

0

使用Parallel.ForEach創建表喜歡

public static bool executeSqlScript(string scriptPath, string serverName, string databaseName) 
    { 
     try 
     { 

      SqlConnection myConn = new SqlConnection("Server=.;Integrated security=SSPI;database=master"); 
      //Server server = new Server(new ServerConnection(myConn)); 
      string CreateCommand = "CREATE DATABASE " + databaseName + ""; 
      string appendText; 
      //delete first line from script(Oldscript) 
      //create db first 
      var myCommand = new SqlCommand(CreateCommand, myConn); 
      myConn.Open(); 
      myCommand.ExecuteNonQuery(); 
      myConn.Close(); 


      List<string[]> list = File.ReadLines(scriptPath) 
       .Select(line => line.ToLower().Split(new string[] { "go" }, StringSplitOptions.None)) 
       .ToList(); 
      Parallel.ForEach(list, (sql) => 
      { 
       using (var mysqlConn = new SqlConnection("Server=.;Integrated security=SSPI;database=master")) 
       { 
        var mysql = "USE [" + databaseName + "]" + Environment.NewLine + string.Join("", sql); 
        var mysqlCommand = new SqlCommand(mysql, mysqlConn); 
        myConn.Open(); 
        mysqlCommand.ExecuteNonQuery(); 
       } 
      }); 

      //server.ConnectionContext.ExecuteNonQuery(readtext); 

      return true; 
     } 
     catch (Exception e) 
     { 
      throw e; 
      return false; 
     } 
    }