Discussion: How to view and get the SQL Server instance name _mssql

Source: Internet
Author: User
Tags microsoft sql server mssqlserver

First, see the example name available

1, the service-sql Server (instance name), the default instance is (MSSQLSERVER)

Or when connecting to enterprise management-View local instances

2. Through the brochure form
Hkey_local_machine/software/microsoft/microsoft SQL server/installedinstance

3, with the command
Sqlcmd/osql
Sqlcmd-l
Sqlcmd-lc
Osql-l

To get the available examples, here's an example of how you can change your situation

Copy Code code as follows:

DECLARE @Table Table (instancename sysname NULL)

Insert @Table EXEC sys.xp_cmdshell ' SQLCMD-LC '

--left (@ @serverName, CHARINDEX ('/', @ @serverName + '/')-1) instead of the name of the machine, according to the case naming rules

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


Second,

--1.
SELECT serverproperty (' InstanceName ')

--2
Sp_helpserver

--3
SELECT @ @SERVERNAME

--4
SELECT * from SYS. sysservers

--5
SELECT * from SYS. SERVERS

Three

EXECUTE xp_regread @rootkey = ' HKEY_LOCAL_MACHINE ',
@key = ' Software/microsoft/microsoft SQL server/instance names/sql ',
@value_name = ' MSSQLServer '

Four

Select case
When Serverproperty (' InstanceName ') is Null Then @ @SERVERNAME
Else serverproperty (' InstanceName ')
End

V. Get all instance names locally or on the network

1, can do with registry reading, like my Code

Copy Code code as follows:

Using System;
Using Microsoft.Win32;

Namespace Smotest
{
Class Program
{
static void Main ()
{
RegistryKey RK = Registry.LocalMachine.OpenSubKey (@ "Software/microsoft/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. Can use SQLDMO.dll to retrieve the list of SQL Server instances. The SQLDMO.dll can is found from the "C:/Program files/microsoft SQL server/80/tools/bin" folder. Refer this assembly into your project and the following snippet would return a List Object containing the SQL Server Instanc Es.
Copy Code code 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 SQL Server in Sqlnamelist)
Sqlservers.add (SQL Server);
}
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.