Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not load type 'Microsoft.SqlServer.Server.SqlContext' from assembly 'System.Data, Version=4.0.0.0

I have facing a problem while i am trying to restore the Data base in sqlserver 2012 .

My project configuration :

ASP.Net core 2.0, Vs2017 Preview2, c#

i have installed Microsoft.SqlServer.Scripting using nuget package , then i tried to restore data base using below code

try
{
    string DatabaseName = "TestDB";
    String ConnectionString = "Data Source=(local);Initial Catalog= " + DatabaseName + ";Integrated Security=SSPI;";
    SqlConnection sqlConnection = new SqlConnection(ConnectionString);
    // Error on the line below
    Microsoft.SqlServer.Management.Common.ServerConnection conn = new Microsoft.SqlServer.Management.Common.ServerConnection(sqlConnection);
    Server srv = new Server(conn);
    Console.WriteLine(srv.Information.Version);
}
catch (Exception ex)
{
    string exepe = ex.Message.ToString();
}

Error is :

Could not load type 'Microsoft.SqlServer.Server.SqlContext' from assembly 'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

Did i miss anything? can you please any body help me out this .

like image 757
Victor Athoti. Avatar asked Sep 02 '25 02:09

Victor Athoti.


1 Answers

I had the same problem today. I solved it by referencing Microsoft.SqlServer.SqlManagementObjects.

like image 167
Shaul Behr Avatar answered Sep 04 '25 16:09

Shaul Behr