0

I'm writing this code:

string sqlConnectionString = conn1;

//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 I'm getting an error in this line

Microsoft.SqlServer.Server = ....

I added the Microsoft.SqlServer.Smo reference, but I get this error:

'Microsoft.SqlServer.Server' is a 'namespace' but is used like a 'type'

What happen? Thanks.

My error:

enter image description here

3
  • What do you actually want to do? Commented Jul 4, 2015 at 5:31
  • @Jamel hi ,why i get error in the Microsoft.SqlServer.Server server = new Microsoft.SqlServer.Server(new ServerConnection(conn)); line? Commented Jul 4, 2015 at 5:33
  • 3
    Because you are using a namespace as a class and making an object of it. Commented Jul 4, 2015 at 5:35

3 Answers 3

4

Change your namespace from Microsoft.SqlServer.Smo to Microsoft.SqlServer.Management.Smo

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

7 Comments

Hi jamal,but i can not found the Microsoft.SqlServer.Management.Smo
Your sql have the library Microsoft.SqlServer.SmoExtended.dll. Search that and add it to your project.
Mostly its in C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies. Tell me when you found it!
i found that and add the reference but so get error!
But i use the Microsoft.SqlServer.SmoExtended.dll
|
2

While using visual studio 2017 & above solutions did not work for me.

Simply I recreated a new project as SQL Server Database Project
and choose SQL CLR C# User Defined Function type
Automatically the namespace got added without any issues.

Previously i tried all nuget packages which did not work for me.

Comments

0

You are initializing a namespace, not a class, that's why error occurred.

Use a class from Microsoft.SqlServer.Server namespace

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.