1、服务—sql Server(实例名),默认实例为(MSsqlSERVER)
@H_502_4@或在连接企业管理时-查看本地实例@H_502_4@
2、通過注冊表
sql Server/InstalledInstance@H_502_4@
3、用命令
sqlcmd/osqlsqlcmd -L
sqlcmd -Lc
osql -L@H_502_4@
获取可用實例,以下舉一個例子,根據自己情況改
insert @Table EXEC sys.xp_cmdshell 'sqlcmd -Lc'@H_502_4@
--LEFT(@@serverName,CHARINDEX('/',@@serverName+'/')-1) 替代為本機名就行了,根據實例命名規則判斷@H_502_4@
SELECT * FROM @Table WHERE instanceName LIKE LEFT( @@serverName,CHARINDEX ( '/',@@serverName + '/' )- 1)+ '%'
@H_502_4@
--1.
@H_502_4@
--2
@H_502_4@
--3
@H_502_4@
--4
@H_502_4@
--5
@H_502_4@
@H_502_4@
EXECUTE xp_regread @rootkey='HKEY_LOCAL_MACHINE',
@key='SOFTWARE/Microsoft/Microsoft sql Server/Instance Names/sql',
@value_name='MSsqlSERVER'@H_502_4@
Select Case
When SERVERPROPERTY ('InstanceName') Is Null Then @@SERVERNAME
Else SERVERPROPERTY ('InstanceName')
End@H_502_4@
1、You can do with registry reading,like my code
using Microsoft.Win32;@H_502_4@
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.
{
NameList sqlNameList = null;
Application app = null;@H_502_4@
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;
}
@H_502_4@ 原文链接:https://www.f2er.com/mssql/63468.html