如何使用vb.net代码获取SQL Server实例路径

编程入门 行业动态 更新时间:2024-10-28 07:27:16
本文介绍了如何使用vb代码获取SQL Server实例路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要使用vb代码在我的机器上安装我的SQL Server实例的完整路径的文件夹。 例如,我已经安装了实例MyComputer \ MyInstanceName 我知道它在C:\Program Files(x86)\ Microsoft SQL Server \ MSSQL.1 \ MSSQL 但是如何使用vb代码获取此路径? 谢谢

解决方案

通过客户端应用程序找出数据库引擎的位置听起来有点奇怪......但是,如果需要找到可执行文件,一种方法是运行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 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实例路径

本文发布于:2023-11-03 12:00:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555100.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   路径   实例   代码   vb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!