2014-11-05 78 views
0

我需要根據文件執行腳本。現在,在大家將這個問題與衆多答案聯繫起來之前,WPF SQL執行腳本文件ADO .net

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Microsoft.SqlServer.Management.Smo; 
using Microsoft.SqlServer.Management.Common; 
using System.IO; 
using System.Data.SqlClient; 

public partial class ExcuteScript : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
    string sqlConnectionString = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ccwebgrity;Data Source=SURAJIT\SQLEXPRESS"; 

    string script = File.ReadAllText(@"E:\Project Docs\MX462-PD\MX756_ModMappings1.sql"); 

    SqlConnection conn = new SqlConnection(sqlConnectionString); 

    Server server = new Server(new ServerConnection(conn)); 

    server.ConnectionContext.ExecuteNonQuery(script); 
    } 
} 

這似乎是基於這個錯誤舊的.net 2.0代碼;

Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information 

現在我很持懷疑態度下井遺留代碼路徑,因爲它咬我太多次了過去。

現在我可以解析出所有的「GO」行並在時間執行每一行,但這似乎有點瘋狂。

在ADO .net 4.0中執行.sql文件的正確方法是什麼?

回答

1

爲什麼要分析出所有的「GO」行並且一次執行每一行都很瘋狂?這可能會讓你花更少的時間來完成這個任務,而不是你花時間寫下你的問題。

  1. 將文件讀入內存。
  2. 將其拆分爲一組獨立的陳述。
  3. 在循環中執行它們。
+0

看起來很瘋狂,不是因爲工作,而是看到在查詢程序中只有一次點擊 – Xaphann 2014-11-06 15:40:50