主頁 > 知識庫 > 探討:如何查看和獲取SQL Server實(shí)例名

探討:如何查看和獲取SQL Server實(shí)例名

熱門標(biāo)簽:承德地圖標(biāo)注公司名需要花錢嗎 北京400電話辦理多少錢 咸陽電腦外呼系統(tǒng)運(yùn)營商 浙江穩(wěn)定外呼系統(tǒng)供應(yīng)商 慶陽地圖標(biāo)注 美團(tuán)地圖標(biāo)注商戶認(rèn)證注冊 榕城市地圖標(biāo)注 電銷外呼系統(tǒng)軟件功能 怎么給高德做地圖標(biāo)注

一、查看實(shí)例名時(shí)可用

1、服務(wù)—SQL Server(實(shí)例名),默認(rèn)實(shí)例為(MSSQLSERVER)

或在連接企業(yè)管理時(shí)-查看本地實(shí)例

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

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

獲取可用實(shí)例,以下舉一個(gè)例子,根據(jù)自己情況改

復(fù)制代碼 代碼如下:

DECLARE @Table TABLE ( instanceName  sysname NULL)

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

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

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

五、在本地或網(wǎng)絡(luò)得到所有實(shí)例名

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

復(fù)制代碼 代碼如下:

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.
復(fù)制代碼 代碼如下:

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

 

標(biāo)簽:新鄉(xiāng) 貴州 上海 重慶 昭通 呼和浩特 江蘇 拉薩

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《探討:如何查看和獲取SQL Server實(shí)例名》,本文關(guān)鍵詞  探討,如何,查看,和,獲取,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《探討:如何查看和獲取SQL Server實(shí)例名》相關(guān)的同類信息!
  • 本頁收集關(guān)于探討:如何查看和獲取SQL Server實(shí)例名的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章