Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3484826
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:50:01+00:00 2026-05-18T10:50:01+00:00

I need to run a sql script that is in a file from my

  • 0

I need to run a sql script that is in a file from my c# application.
The problem is that it creates objects an executes commands on different databases.

In the script, every time i need to change the current database it uses the command

USE [databasename]

and since there are many object creations it uses the command GO a lot of times, so I read the file until i find a GO command and run the sentence in the database; so far so good, but after the command runs the connection returns to the default database (master in this case) ignoring the last USE sentence.

This is the code I use to read the file and execute the script.

using (StreamReader reader = new StreamReader(databaseScriptFile))
  {
      DatabaseFactory.CreateDatabase()
      Database db = new SqlDatabase(connectionstring);
      txbuilder = new StringBuilder();
      bool lastExecute = true;               
      while (!reader.EndOfStream)
      {
          text = reader.ReadLine();
          if (text.Trim().ToUpper() != "GO")
          {
              lastExecute = false;
              txbuilder.AppendLine(text);
          }
          else
          {
              lastExecute = true;
              db.ExecuteNonQuery(CommandType.Text, txbuilder.ToString());
              txbuilder.Length = 0;
          }
      }
      //Make sure that the last sentence is executed
      if (!lastExecute)
          db.ExecuteNonQuery(CommandType.Text, txbuilder.ToString());
  }

It seems that it clears the connection after each ExecuteNonQuery command executing sp_reset_conection (that’s what I get in the profiler)

Is there any elegant and simple way to acomplish this?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:50:02+00:00Added an answer on May 18, 2026 at 10:50 am

    There’s a good investigation by Joshua Flanagan with test harness and results on his blog. It reads as if he was having a similar problem to what you describe.

    There’s two ways to do it, and you’ll have to evaluate which is the best for you, and whether either have any knock-on effects.

    1. Explictly open and close the database connection. So rather than just open the database with SqlDatabase, you’ll need to create a SqlConnection and call Open() on it.
    2. Simply add Connection Reset=False onto your connection string. I don’t know the pros and cons of this approach however, so don’t know if there’s any side-effects of this as I understand this can disable connection pooling. You’ll know better than I will within the context of your application/utility.

    I suggest reading the blog article for all the gory details, then do a little bit of digging around to see which is the best for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to run a stored procedure from a C# application. I use the
I need to run 8-10 instances of my application on IIS 6.0 that are
what snapins or initializations do I need to run a sql server powershell script
I am using Installshield 2008 Developer Edition and need to run SQL scripts against
I need to run an equipment audit and to do that I need to
I need to run a Linux CLI command and get its stdout output from
I need to run a RLIKE query on a database mmoved from MySQL to
I need to pull some BLOB data from a SQL Server 2005 database and
I need to run .Net code in Sql and I'm trying to decide between
I need to run a JavaScript function onLoad(), but only do it if the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.