Reputation: 3677
I need to execute a script based on a file. Now before everyone links me to multitude answers of this;
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);
}
}
This seems to be old .net 2.0 code based on this error;
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
Now I am very leery to go down the legacy code path, as it has bite me one too many times in the past.
Now I could parse out all the "GO" lines and execute each one at time but that seems kind of crazy.
What is the correct way to execute a .sql file in ADO .net 4.0?
Upvotes: 0
Views: 208
Reputation: 29421
Why is parsing out all the "GO" lines and executing each one at a time crazy? It would probably take you less time to do that than it took you to write your question.
Upvotes: 1