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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
behzad razzaqi
  • 1,503
  • 2
  • 18
  • 33

3 Answers3

4

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

Jamil
  • 330
  • 1
  • 6
  • 25
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.

SenthilPrabhu
  • 661
  • 6
  • 29
0

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

Use a class from Microsoft.SqlServer.Server namespace

Hemal
  • 3,682
  • 1
  • 23
  • 54