views:

448

answers:

3

I need SQL server 2005 instance name from Adodb connection object in c#. Please Help for my query.

Thanks in advance

A: 

Try to run:

SELECT @@ServerName  AS ServerName,
       @@ServiceName AS ServiceName
Rubens Farias
A: 

If you are stepping through C# code that is making a call to the database, and you don't know where it is getting the connection string from, you can set a breakpoint in the code right around the place where it makes the database call. Then, you can examine the properties of the various objects that are present. For example, check the Connection property of the SqlCommand. The database instance will be included in the connection string.

DOK
+1  A: 

The ADODB connection itself doesn't have that information avaiable.

You can either run the SQL query:

SELECT SERVERPROPERTY('instancename') 

using your connection, or you can use the SMO (SQL Server Management Objects) to get that information:

using(SqlConnection _con = new SqlConnection(your-connection-string))
{
   string instanceName = new Microsoft.SqlServer.Management.Smo.Server
                            (new ServerConnection(_con)).InstanceName;
}
marc_s