探讨:如何查看和获取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;
}
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程