我试图确定我已经安装的sql server/sql express的实例(手动或编程),但所有的例子都告诉我运行一个sql查询来确定这假设我已经连接到一个特定的实例。
当前回答
SQL Server浏览器服务http://msdn.microsoft.com/en-us/library/ms181087.aspx
其他回答
这里有一个简单的方法: 去 然后开始 程序然后 然后是SQL Server 2005 配置工具 然后是SQL Server配置管理器 然后进行SQL Server 2005网络配置 在这里,您可以找到安装到您的机器上的所有实例。
SQL Server permits applications to find SQL Server instances within the current network. The SqlDataSourceEnumerator class exposes this information to the application developer, providing a DataTable containing information about all the visible servers. This returned table contains a list of server instances available on the network that matches the list provided when a user attempts to create a new connection, and expands the drop-down list containing all the available servers on the Connection Properties dialog box. The results displayed are not always complete. In order to retrieve the table containing information about the available SQL Server instances, you must first retrieve an enumerator, using the shared/static Instance property:
using System.Data.Sql;
class Program
{
static void Main()
{
// Retrieve the enumerator instance and then the data.
SqlDataSourceEnumerator instance =
SqlDataSourceEnumerator.Instance;
System.Data.DataTable table = instance.GetDataSources();
// Display the contents of the table.
DisplayData(table);
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}
private static void DisplayData(System.Data.DataTable table)
{
foreach (System.Data.DataRow row in table.Rows)
{
foreach (System.Data.DataColumn col in table.Columns)
{
Console.WriteLine("{0} = {1}", col.ColumnName, row[col]);
}
Console.WriteLine("============================");
}
}
}
来自MSDN http://msdn.microsoft.com/en-us/library/a6t1z9x2(v=vs.80).aspx
我知道这个帖子有点老了,但在我找到我正在寻找的答案之前,我就看到了这个帖子,我想分享一下。如果您正在使用SQLExpress(或localdb),有一种更简单的方法来查找实例名。 在命令行类型中:
> sqllocaldb i
这将列出您在本地安装的实例名。因此,您的完整服务器名称应该在要连接的实例名称前面包含(localdb)\。此外,sqllocaldb允许您创建或删除实例以及配置它们。参见:SqlLocalDB Utility。
SQL Server浏览器服务http://msdn.microsoft.com/en-us/library/ms181087.aspx
你可以查询这个注册表值直接得到SQL版本:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\90\Tools\ClientSetup\CurrentVersion
或者你可以查询你的实例名,然后使用sqlcmd和你想要的实例名:
要查看实例名:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names
然后执行这个:
SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition')
如果您使用c++,您可以使用这段代码来获取注册表信息。
推荐文章
- 确定记录是否存在的最快方法
- 从现有模式生成表关系图(SQL Server)
- 我如何循环通过一组记录在SQL Server?
- 数据库和模式的区别
- 如何在SQL Server中一次更改多个列
- 外键约束可能导致循环或多条级联路径?
- 如何选择每一行的列值不是独特的
- nvarchar(max)非文本
- 在SQL Server 2008 R2中重命名数据库时出错
- 将数据复制到另一个表中
- 如何在SQL中选择表的最后一条记录?
- 修改列,添加默认约束
- 在存储过程中使用“SET XACT_ABORT ON”有什么好处?
- 如何检查SQL Server文本列是否为空?
- 如何创建一个SQL Server函数“连接”多行从一个子查询到一个单独的分隔字段?