2014-11-05 60 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