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=SURAJITSQLEXPRESS";
    string script = File.ReadAllText(@"E:Project DocsMX462-PDMX756_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.net4.0中执行.sql文件的正确方法是什么?

为什么解析出所有"GO"行并一次执行每一行都很疯狂?这可能比你写问题花的时间更少。

  1. 将文件读入内存
  2. 将其拆分为一组独立的语句
  3. 在循环中执行它们

最新更新