探讨:如何查看和获取SQL Server实例名

(编辑:jimmy 日期: 2024/12/24 浏览:2)

一、查看實例名時可用

1、服务—SQL Server(实例名),默认实例为(MSSQLSERVER)

或在连接企业管理时-查看本地实例

2、通過注冊表
HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Microsoft SQL Server/InstalledInstance

3、用命令
sqlcmd/osql
sqlcmd -L
sqlcmd -Lc
osql -L

获取可用實例,以下舉一個例子,根據自己情況改
复制代码 代码如下:
DECLARE @Table TABLE ( instanceName  sysname NULL)

insert @Table EXEC sys.xp_cmdshell 'sqlcmd -Lc'

--LEFT(@@serverName,CHARINDEX('/',@@serverName+'/')-1) 替代為本機名就行了 , 根據實例命名規則判斷

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

二、

--1.
SELECT SERVERPROPERTY('InstanceName')

--2
sp_helpserver

--3
select @@SERVERNAME

--4
SELECT * FROM SYS.SYSSERVERS

--5
SELECT * FROM SYS.SERVERS

三、

EXECUTE xp_regread @rootkey='HKEY_LOCAL_MACHINE',
@key='SOFTWARE/Microsoft/Microsoft SQL Server/Instance Names/SQl',
@value_name='MSSQLSERVER'

四、

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

五、在本地或网络得到所有实例名

1、You can do with registry reading , like my code
复制代码 代码如下:
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、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 would return a List Object containing the sql server instances.
复制代码 代码如下:
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;
}

 

一句话新闻

一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?