如何使用vb.net代码获取SQL Server实例路径 [英] How do I get SQL Server Instance path using vb.net code
问题描述
我需要使用vb代码在我的机器上安装我的SQL Server实例的完整路径的文件夹。
例如,我已经安装了实例MyComputer \ MyInstanceName
我知道它在C:\Program Files(x86)\ Microsoft SQL Server \ MSSQL.1 \ MSSQL
但是如何使用vb.net代码获取此路径?
谢谢
通过客户端应用程序找出数据库引擎的位置听起来有点奇怪......但是,如果需要找到可执行文件,一种方法是运行WMI查询。对于已安装的应用程序,您可以使用 Win32_Product [ ^ ]由于每个实例都是一个单独的服务,你可以使用 Win32_Service [ ^ ]服务。
关于查询,请查看使用Windows Management Instrumentation调用获取系统信息 [ ^ ]
连接到SQL实例并运行此查询:
SELECT name FROM master.sys.dm_os_loaded_modules WHERE description LIKE ' SQL Server%' AND LIKE ' %。exe'
它返回绝对路径实例正在运行的EXE。
Imports Microsoft.SqlServer.Management.Smo
Imports System.Data.SqlClient
Public 类 Form1
' 首先提供对Microsoft.SqlServer.smo的引用
' Microsoft.SqlServer.ConnectionInfo
此后添加上述命名空间
私有 Sub Button1_Click( ByVal sender 正如系统。对象, ByVal e As System.EventArgs)句柄 Button1.Click
Dim vrInstanceName 作为 字符串 = EZIERPSERVER
Dim sqlConnection 作为 新 SqlConnection( Integrated Security = SSPI;数据源=(本地)\& vrInstanceName)
Dim serverConnection As 新 Microsoft.SqlServer.Management.Common.ServerConnection(sqlConnection)
Dim server = 新服务器(serverConnection)
MsgBox(server.Information.RootDirectory)
结束 Sub
结束 类
Hi,
I need to get the folder with full path of my SQL Server instance installed on my machine, using vb code.
For example, I have installed instance MyComputer\MyInstanceName
I know it is in C:\Program Files (x86)\Microsoft SQL Server\MSSQL.1\MSSQL
but how can I get this path using vb.net code?
Thanks
Finding out the location of the database engine by a client application sounds a bit odd... However, if you need to locate the executables, one way is to run a WMI query. For installed applications you can use Win32_Product[^] and since each instance is a separate service, you can use Win32_Service[^] for the services.
About the queries, have a look at Obtain System Information Using Windows Management Instrumentation Calls[^]
Connect to teh SQL instance and run this query:
SELECT name FROM master.sys.dm_os_loaded_modules WHERE description LIKE 'SQL Server%' AND name LIKE '%.exe'It returns the absolute path to the EXE that the instance is running.
Imports Microsoft.SqlServer.Management.Smo Imports System.Data.SqlClient Public Class Form1 'First give reference to Microsoft.SqlServer.smo 'Microsoft.SqlServer.ConnectionInfo 'After this add above namespaces Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim vrInstanceName As String = "EZIERPSERVER" Dim sqlConnection As New SqlConnection("Integrated Security=SSPI; Data Source=(local)\" & vrInstanceName) Dim serverConnection As New Microsoft.SqlServer.Management.Common.ServerConnection(sqlConnection) Dim server = New Server(serverConnection) MsgBox(server.Information.RootDirectory) End Sub End Class
这篇关于如何使用vb.net代码获取SQL Server实例路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!