您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關SQL Server中怎么查看和獲取實例名,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
一、查看實例名時可用
1、服務—SQL Server(實例名),默認實例為(MSSQLSERVER)
或在連接企業管理時-查看本地實例
2、通過注冊表HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Microsoft SQL Server/InstalledInstance
3、用命令sqlcmd/osqlsqlcmd -Lsqlcmd -Lcosql -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')
--2sp_helpserver
--3select @@SERVERNAME
--4SELECT * FROM SYS.SYSSERVERS
--5SELECT * FROM SYS.SERVERS
三、
EXECUTE xp_regread @rootkey='HKEY_LOCAL_MACHINE',@key='SOFTWARE/Microsoft/Microsoft SQL Server/Instance Names/SQl',@value_name='MSSQLSERVER'
四、
Select CaseWhen SERVERPROPERTY ('InstanceName') Is Null Then @@SERVERNAMEElse 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;}
關于SQL Server中怎么查看和獲取實例名就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。