如何查看和获取SQL Server实例名
4个回答
2014-12-04 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517174
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
一、查看实例名时可用
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;
}
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;
}
展开全部
一、
查看实例名时可用
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);
}
}
}
}
}
查看实例名时可用
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);
}
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
select SERVERPROPERTY('servername')
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SELECT @@SERVERNAME
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |