I want to be able to create a database in my C # WinForm application using the code I found HERE .
But I need to find a way to get the default data directory for this particular instance of SQL Server. I am wondering if there was an easy way to achieve this that can be used for different versions of SQL Server.
Thanks in advance.
EDIT
I found the following Select, which will return the default data directory on the remote server:
SELECT
SUBSTRING(physical_name, 1, CHARINDEX(N'master.mdf', LOWER(physical_name)) - 1)
FROM master.sys.master_files
WHERE database_id = 1
AND file_id = 1
This solution will only work on SQL Server 2005+.
**
source
share