开发者

Getting the default SQL Server directory in VB.Net

I guess the default path for the SQL Server files is located at:

C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA

Is there any way of reading the default dire开发者_开发知识库ctory with the .NET Framework?


The following will get you the installation folder (ie. C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL) for the SQL Server that you specify in the connection string:

Dim connectionString As String = "whatever..."
Dim serverConnection As New ServerConnection(connection)
Dim server As New Server(serverConnection)
Dim installationDirectory As String = server.Information.RootDirectory

These classes are all part of SQL Server Management Objects (SMO).

The MSDN docs for RootDirectory can be found here. Specifying the correct SQL Server instance in the connection string supplied to the ServerConnection will ensure you get the directory for the correct instance.


You can read files and directories using the shared methods of the File and Directory classes.

You can find the path of a specific database by running the following SQL: SELECT Filename FROM Sysdatabases WHERE name = 'MyDB'.


Here's a link:

The value's actual registry location will depend upon your setup and the versions of SQL that you have installed. The tag you're looking for is SQLDataRoot. On my machine, it's located in the following node:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\Setup

So you can just loop through the registry using .Net library, obtain the relevant key, probe into it, obtain further keys until you hit the correct keys and the correct values.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜