欢迎来到代码驿站!

MsSql

当前位置:首页 > 数据库 > MsSql

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

时间:2022-01-27 08:48:59|栏目:MsSql|点击:

一、查看??例名?r可用

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

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

2、通?^注?员?
HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Microsoft SQL Server/InstalledInstance

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

获取可用??例,以下?e一??例子,根??自己情?r改

复制代码 代码如下:

DECLARE @Table TABLE ( instanceName  sysname NULL)

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

--LEFT(@@serverName,CHARINDEX('/',@@serverName+'/')-1) 替代?楸?C名就行了 , 根????例命名??t判??

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

 

上一篇:Filestream使用简单步骤总结

栏    目:MsSql

下一篇:SQLServer CONVERT 函数测试结果

本文标题:探讨:如何查看和获取SQL Server实例名

本文地址:http://www.codeinn.net/misctech/191525.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有