我需要根据文件执行脚本。现在,每个人都将我与众多答案联系起来;
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文件的正确方法是什么?
答案 0 :(得分:1)
为什么解析出所有“GO”行并一次执行每一行疯狂?这可能比你写问题花费的时间更少。