0

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?

1 Answer 1

1

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.

  1. Read the file into memory.
  2. Split it into a set of independent statements.
  3. Execute them in a loop.
Sign up to request clarification or add additional context in comments.

1 Comment

It seems crazy not because of the work but seeing that there is one click in a query program

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.