开发者

Get Sql Server instance name using Adodb conncection object

开发者 https://www.devze.com 2022-12-15 10:54 出处:网络
I need SQL server 2005 instance name from Adodb connection object in c#. Please Help for 开发者_如何学Pythonmy query.

I need SQL server 2005 instance name from Adodb connection object in c#. Please Help for 开发者_如何学Pythonmy query.

Thanks in advance


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;
}


Try to run:

SELECT @@ServerName  AS ServerName,
       @@ServiceName AS ServiceName


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号