Get "Microsoft.SqlServer.Wmi.ServiceState" in .NET 4.0 Application - c#

I'm connection to an Microsoft SQL Server 2008 in C# .NET 4.0 Application.
Now I want to display all sql Services from the remote computer.
ManagedComputer managedComputer = new ManagedComputer("ServerName", "Login", "Password");
foreach (var service in managedComputer.Services)
{
Console.WriteLine(service.Name + " | " + service.ServiceState);
}
The problem is, that I can't build the solution because Visual Studio 2012 brings the error, I should add an Assembly. Here is the error message:
The type 'Microsoft.SqlServer.Management.Smo.Wmi.ServiceState' is defined in an assembly that is not referenced. You must add a
reference to assembly 'Microsoft.SqlServer.WmiEnum, Version=11.0.0.0,
Culture=neutral, PublicKeyToken=89845dcd8080cc91'.
Of course I added this assembly but still there is no change.
The problem comes from the Code service.ServiceState.
How can I fix this problem? Is this Assembly only for .NET 2.0?
Or is there any other way to get the status (running, stopped...) of an remote sql service?

Related

SQL Server and Visual Studio: how to cope with different Microsoft.SqlServer.Types versions

In Visual Studio (2019) I've made an assembly that is using Microsoft.SqlServer.Types, and I compiled it without error messages.
Now I try to import this assembly into my SQL Server (2019) database with T-SQL, but this message appears:
Msg 10300, Level 16, State 2, Line 1 Assembly "MyAssembly" references assembly 'microsoft.sqlserver.types, version=15.100.0.0,
culture=neutral, publickeytoken=89845dcd8080cc91.', which is not
present in the current database. SQL Server attempted to locate and
automatically load the referenced assembly from the same location
where referring assembly came from, but that operation has failed
(reason: another assembly with same SQL name is already present in the
current database). Please load the referenced assembly into the
current database and retry your request.
In SSMS (18.5.1) I see that the microsoft.sqlserver.types that SQL Server is using has a different version: 15.0.0.0.
If I try to remove this from SQL Server before importing the 15.100.0.0 version I get a message saying that deleting is not possible because it is a system assembly.
What is the best I can do now? I am brand new to this stuff.

Why C# does not recognize Server() even after adding assembly "Microsoft.SqlServer.ConnectionInfo"

The type or namespace name 'Server' could not be found (are you missing a using directive or an assembly reference?)
string connetionString = #"connectionStirng";
SqlConnection conn = new SqlConnection(connetionString);
var server = new Server(new ServerConnection(conn));
added at the top
using Microsoft.SqlServer.Management.Common;
Still giving error.
Any help appricated.
Already checked:
Why i'm getting error for the Microsoft.SqlServer.Server namespace?
I can't add Microsoft.SqlServer.Management.Common to my ASP.NET MVC Application
Install this nuget package:
https://www.nuget.org/packages/Microsoft.SqlServer.SqlManagementObjects
Then add using directive:
using Microsoft.SqlServer.Management.Smo;
Now you can create Server object.
var server = new Server();
Installing SMO
Beginning with SQL Server 2017 SMO is distributed as the
Microsoft.SqlServer.SqlManagementObjects NuGet package to allow users
to develop applications with SMO.
This is a replacement for SharedManagementObjects.msi, which was
previously released as part of the SQL Feature Pack for each release
of SQL Server. Applications that use SMO should be updated to use the
NuGet package instead and will be responsible for ensuring the
binaries are installed with the application being developed.
https://learn.microsoft.com/en-us/sql/relational-databases/server-management-objects-smo/installing-smo?view=sql-server-2017
As suggested by Han, Please check the assembly reference path and make sure that the file is there at the referenced location. I had faced similar problem, this was working on the Development environment but error on test server. The reason was the SQL Server Management studio was not installed on the Test server. So i copied the required Assemblies from dev machine to bin folder of the application on test server and that worked.
Can you try adding a reference to:
Microsoft.SqlServer.dac.dll
If cant find the dll, then need to install SSDT (Sql Server Data Tools).

Could not load file or assembly 'Microsoft.SqlServer.BatchParser

Currently we moved our Web Applications to a new server so now we have a Database and Web + App Server. Before moving everything was working fine.
But now as we have installed SQL Server on the DB Server we are getting errors related to:
Microsoft.SqlServer DLL files.
So basically the code runs a script on the DB server to create a New Database.
First I was getting the Error, related to: Microsoft.SqlServer.ConnectionInfo.dll
So I installed, the nuget Package: Shared Management Objects
Which seemed to have fix the Problem.
BUT now I am getting an error related to Microsoft.SqlServer.BatchParser
I followed the links online and they suggest to install:
Shared Management Objects from here; https://www.microsoft.com/en-pk/download/confirmation.aspx?id=29065
Note; we are using SQL server 2012 standard.
So I installed Shared Management Objects and also restarted the server, but I am still getting the same error.
Could not load file or assembly 'Microsoft.SqlServer.BatchParser, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.
If I navigate to: C:\Windows\assembly I can see that Microsoft.SqlServer.BatchParser:
The Exception occurs on this line of code:
server.ConnectionContext.ExecuteNonQuery(script);

Database Project, Assembly was not found in the SQL catalog

I have a SQL Server Database project in Visual Studio 2013. This project refer to another project called BusinessLayer.
BusinessLayer project has reference to System.Component.DataAnnotations and DataAnnotations is in .NET framework dll folder "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\System.ComponentModel.DataAnnotations.dll"
I also write a SQL CLR Trigger in DB Project, and when I deploy the database project, I got this error:
SQL72014: .Net SqlClient Data Provider: Msg 6503, Level 16, State 12, Line 1
Assembly 'system.componentmodel.dataannotations, version=4.0.0.0, culture=neutral, publickeytoken=31bf3856ad364e35.' was not found in the SQL catalog.
I tried to get the public key token of the dll and saw it is same as in the error.
So what is this error and how to solve it?
I'm open to suggestions.

Could not load file or assembly 'Microsoft.SqlServer.Smo, Version=10.0.0

OS : Microsoft Windows NT 5.2.3790 Service Pack 2 Version: 2.0.50727.42
Doing: creating Tables...
Rec: system.m.FileNotFoundexception: could not load file or assembly
'microsoft.sqlserver.smo, version=10.0.0.0, culture=neutral File name:
'microsoft.sqlserver.smo, version=10.0.0.0, culture=neutral,
PublicKeyToken=89845dcd8080cc91' at
claimsure.Forml.Runsqlscript(string connstring, streamReader sr) at
myapp.Forml.bw_Dowork(object sender, DoworkeventArgs e) 'MN: Assembly
binding logging is turned OFF. To enable assembly bind failure
logging, set the registry value
(NKLm\software\microsoft\Fusion!enableLog] (DwoRD) to 1. Note: There
is some performance penalty associated with assembly bind failure
logging. To turn this feature off, remove the registry value
(NKLm\software\microsoft\Fusion!enableLog).
I get the above error when running my application on a Windows 2003 server with SQL Server 2005.
I know there is a solution to download SQLServer2005_XMO from http://www.microsoft.com/en-us/download/details.aspx?id=24793
I have done this, but after the install a server reboot is required, which is not feasible for the time being.
I need to make use of the server object.
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using (SqlConnection connection = new SqlConnection(connString))
{
Server server = new Server(new ServerConnection(connection));
server.ConnectionContext.StatementTimeout = 2400;
server.ConnectionContext.ExecuteNonQuery(sqlString);
}
Is is possible to load the dll in the same directory as my app and reference it from within the code?
Thanks.
edit: added full error
Resolved this by building an MSI installer and including all the DLL's required.
Thanks for the pointers.

Categories

Resources