Click here to Skip to main content
14,732,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have one SQL server script file. I want to execute this script file through C#.

I used the following code.
SqlConnection conn = new SqlConnection();
                conn = new SqlConnection(SQLConnection);
                Server server = new Server(new ServerConnection("ConnectionString"));

For this I loaded the dll from "C:\\ProgramFiles\\MicrosoftSqlServer\\100\\SDK\\Asseblies".

But I am getting error like mixed mode .net version 'v2.0.02527' to .net version 'v4.0'.

So how to execute the script file using ExecuteNonQuery or some other way?

Please help me

Thank you
Updated 25-Nov-11 5:12am
Timberbird 25-Nov-11 10:35am
Why loading the DLL manually? Try to demove it from project and add standard Systed.Data assembly (from the "Add Reference" dialog, ".NET" tab)
Member 7684075 26-Nov-11 7:28am
The Server and ServerConnection class i am using Microsoft.Management.Common. The normat Syste.Data dont have Server and ServerConnection class. That is the reason i added the dll from C:\\ProgramFiles\\MicrosoftSqlServer\\100\\SDK\\Asseblies. I am find this reference from .net tab.

And one more thing this is i am using in windows application. So if i execute this page it will work fine.

The same form i ll include with installer (web setup project installer). When installer is starting i ll call windows application. This situation only not working.

SqlConnection are built into .net so all you need is the System.Data reference in your project.

You do not need to reference SQL specific assemblies.
Few things here. Firstly as Timerbird says, that's not the way to add a reference. Just add System.Data from .NET in references. If you're using .NET4 make sure your not using the Client Profile, as that probably won't have it. Doesn't seem to have anything of any use.

I don't believe you can run a whole script as a single command like that. A script is usually a sequence of commands individually invoked by the keyword 'go'. Think you'll have issues if running multiple commands. Either shell out to osql to do it, or split the script on gos and submit each command individually.

EDIT: Yes, as pointed out above, you don't need to add any reference thinking about it. It's already there....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900