8

I'm write this code for run sql server script:

string sqlConnectionString = "Data Source=.;Initial Catalog=behzad;Integrated Security=True";
//string sqlConnectionString = "Data Source=(local);Initial Catalog=AdventureWorks;Integrated Security=True";
FileInfo file = new FileInfo("d:\\behzadBULK.sql");
string script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(sqlConnectionString);
Microsoft.SqlServer.Server server = new Microsoft.SqlServer.Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);


but this line :

Microsoft.SqlServer.Server server = new Microsoft.SqlServer.Server(new ServerConnection(conn));

i get this error:

 Are you missing reference?


what reference should be add to the project?

10
  • Use using System.Data.SqlClient; on namespace section.. Commented May 23, 2015 at 5:51
  • See this msdn.microsoft.com/en-us/library/… Commented May 23, 2015 at 5:52
  • I added using System.Data.SqlClient; but so get up error! Commented May 23, 2015 at 5:55
  • What reference does it show in the error ? Commented May 23, 2015 at 5:59
  • 1
    Just install the reference to Microsoft.SqlServer.Server in your project, if its not present now. And add a namespace for it in your code this should do it Commented May 23, 2015 at 6:02

2 Answers 2

3

Your problem here is that a reference to the assembly Microsoft.SqlServer.Server is missing and that's why that error is popping up.

You can resolve it by just adding a reference to your project for that particular assembly by right clicking your project and clicking on add reference. That should open a window to show you all the assemblies available and from there you can choose this assembly and add it to your project.

After that, make sure you have added the namespace for it in your code and that should do it.

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

3

Try Using SQLclient Connection Not SqlServer

using System.Data;
using System.Data.SqlClient;

and Some code like this

settings = System.Configuration.ConfigurationManager.ConnectionStrings["MyTweetConnection"];
ConnectionString = settings.ConnectionString;
SQLCon = new SqlConnection(ConnectionString);

Comments

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.