zoukankan      html  css  js  c++  java
  • 如何查看SQL Server的实例名(转载)

    一、

    查看實例名時可用 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;
    }

    作者:立雪三尺
    出处:http://www.cnblogs.com/songsz1/
    我滴生命是如此滴辉煌!我滴生活是如此滴灿烂!
    关于作者:初出茅庐,职场菜鸟。静如瘫痪,动若癫痫。!姓名不知,生死不详! 如有问题或建议,请多多赐教! 如无,出门左转是政府。
    本文版权归作者所有,欢迎转载,不一定非得在显眼处给出原文链接
    如有想不开的暴击我
    最后严重警告:本文作者真的是名程序员。

  • 相关阅读:
    如何确定Kafka的分区数、key和consumer线程数
    Storm程序永久代内存溢出
    JVM堆内存相关的启动参数:年轻代、老年代和永久代的内存分配
    Git添加本地项目出现fatal: unable to get credential storage lock: File exists
    Kafka的通讯协议
    文件上传 数据对比
    jquery 弹出层
    XSS 简单理解之:AntiSamy
    XSS 简单理解
    超人说我最叼蝙蝠侠笑了 超级英雄战力排名
  • 原文地址:https://www.cnblogs.com/songsz1/p/2833862.html
Copyright © 2011-2022 走看看