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

  • SEARCH
  • Home
  • 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 364059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:24:07+00:00 2026-05-12T13:24:07+00:00

For some integration tests I want to connect to the database and run a

  • 0

For some integration tests I want to connect to the database and run a .sql file that has the schema needed for the tests to actually run, including GO statements. How can I execute the .sql file? (or is this totally the wrong way to go?)

I’ve found a post in the MSDN forum showing this code:

using System.Data.SqlClient;
using System.IO;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string sqlConnectionString = "Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True";
            FileInfo file = new FileInfo("C:\\myscript.sql");
            string script = file.OpenText().ReadToEnd();
            SqlConnection conn = new SqlConnection(sqlConnectionString);
            Server server = new Server(new ServerConnection(conn));
            server.ConnectionContext.ExecuteNonQuery(script);
        }
    }
}

but on the last line I’m getting this error:

System.Reflection.TargetInvocationException:
Exception has been thrown by the
target of an invocation. —>
System.TypeInitializationException:
The type initializer for ”
threw an exception. —>
.ModuleLoadException:
The C++ module failed to load during
appdomain initialization. —>
System.DllNotFoundException: Unable to
load DLL ‘MSVCR80.dll’: The specified
module could not be found. (Exception
from HRESULT: 0x8007007E).

I was told to go and download that DLL from somewhere, but that sounds very hacky. Is there a cleaner way to? Is there another way to do it? What am I doing wrong?

I’m doing this with Visual Studio 2008, SQL Server 2008, .Net 3.5SP1 and C# 3.0.

  • 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-12T13:24:07+00:00Added an answer on May 12, 2026 at 1:24 pm
    using Microsoft.SqlServer.Management.Common;
    using Microsoft.SqlServer.Management.Smo;
    

    You shouldn’t need SMO to execute queries. Try using the SqlCommand object instead. Remove these using statements. Use this code to execute the query:

     SqlConnection conn = new SqlConnection(sqlConnectionString);
     SqlCommand cmd = new SqlCommand(script, conn);
     cmd.ExecuteNonQuery();
    

    Also, remove the project reference to SMO. Note: you will want to clean up resources properly.

    Update:

    The ADO.NET libraries do not support the ‘GO’ keyword. It looks like your options are:

    1. Parse the script. Remove the ‘GO’ keywords and split the script into separate batches. Execute each batch as its own SqlCommand.
    2. Send the script to SQLCMD in the shell (David Andres’s answer).
    3. Use SMO like the code from the blog post.

    Actually, in this case, I think that SMO may be the best option, but you will need to track down why the dll wasn’t found.

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

Sidebar

Related Questions

Using Visual Studio 2010, I have written a simple WCF service and some integration
I want to make sure I am testing Models/Objects in isolation and not as
Imagine two servers (each within an own jvm process) which communicate using some form
I'm developing an application for measuring and storing running/cycling tracks using OSM/Google Maps integration.
I have an app that does an iteration to create points on a graph
I'm working on a project that involves diverting phone calls to a number of
We use liquibase to keep track of our database changes.. First changeSet contains those
So basically i have a domain object and a generic repository that can do
I've got a Windows 7 machine upon which I do both PHP/MySql Dev, and
after reading a few articles, I am still not sure I understand Multi-threading to

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.