Discussion: how to view and obtain the SQL Server instance name

Source: Internet
Author: User
Tags mssqlserver

I. Available when viewing the Instance name

1. Service-SQL Server (Instance name), default instance: (MSSQLSERVER)

Or you can view the local instance when you connect to enterprise management.

2. Through the notebooks table
HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft SQL Server/InstalledInstance

3. Use commands
Sqlcmd/osql
Sqlcmd-L
Sqlcmd-Lc
Osql-L

Obtain the availability example. The following is an example.

Copy codeThe Code is as follows: DECLARE @ Table TABLE (instanceName sysname NULL)

Insert @ Table EXEC sys. xp_mongoshell 'sqlcmd-Lc'

-- LEFT (@ serverName, CHARINDEX ('/', @ serverName + '/')-1) Replace the current host name, root verification instance naming rules

SELECT * FROM @ Table WHERE instanceName like left (@ serverName, CHARINDEX ('/', @ serverName + '/')-1) + '%'

II,

-- 1.
Select serverproperty ('instancename ')

-- 2
Sp_helpserver

-- 3
Select @ SERVERNAME

-- 4
SELECT * from sys. SYSSERVERS

-- 5
SELECT * from sys. SERVERS

III,

EXECUTE xp_regread @ rootkey = 'HKEY _ LOCAL_MACHINE ',
@ Key = 'Software/Microsoft SQL Server/Instance Names/SQL ',
@ Value_name = 'mssqlserver'

IV,

Select Case
When SERVERPROPERTY ('instancename') Is Null Then @ SERVERNAME
Else SERVERPROPERTY ('instancename ')
End

5. Obtain the names of all instances locally or on the network.

1. You can do with registry reading, like my code

Copy codeThe Code is as follows: using System;
Using Microsoft. Win32;

Namespace SMOTest
{
Class Program
{
Static void Main ()
{
RegistryKey rk = Registry. LocalMachine. OpenSubKey (@ "SOFTWARE/Microsoft SQL Server ");
String [] instances = (String []) rk. GetValue ("InstalledInstances ");
If (instances. Length> 0)
{
Foreach (String element in instances)
{
If (element = "MSSQLSERVER ")
Console. WriteLine (System. Environment. MachineName );
Else
Console. WriteLine (System. Environment. MachineName + @ "/" + element );
}
}
}
}
}

2. You can use SQLDMO. dll to retrieve the list of SQL Server instances. the SQLDMO. dll can be found from the "C:/Program Files/Microsoft SQL Server/80/Tools/Bin" folder. refer this assembly in your project and the following snippet wocould return a List Object containing the SQL server instances.Copy codeThe Code is as follows: public static List GetSQLServerInstances ()
{
NameList sqlNameList = null;
Application app = null;

Var sqlServers = new List ();
Try
{
App = new ApplicationClass ();
SqlNameList = app. ListAvailableSQLServers ();
Foreach (string sqlServer in sqlNameList)
SqlServers. Add (sqlServer );
}
Catch (Exception ex)
{
// Play with the exception.
}
Finally
{
If (sqlNameList! = Null)
SqlNameList = null;
If (app! = Null)
App = null;
}
Return sqlServers;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.