C# SQL Restore database to default data location -


i'm writing c# application downloads compressed database backup via ftp. application needs extract backup , restore default database location.

i not know version of sql server installed on machine application runs. therefore, need find default location based on instance name (which in config file).

the examples found had registry key read, not work, since assumes 1 instance of sql installed.

another example found created database, read database's file properties, deleting database once done. that's cumbersome.

i did find in .net framework should work, ie:

microsoft.sqlserver.management.smo.server(servername).settings.defaultfile

the problem returning empty strings, not help.

i need find out nt account under sql service running, can grant read access user on backup file once have extracted.

what discovered

microsoft.sqlserver.management.smo.server(servername).settings.defaultfile 

only returns non-null when there no path explicitly defined. specify path not default, function returns path correctly.

so, simple workaround check whether function returns string, or null. if returns string, use that, if it's null, use

microsoft.sqlserver.management.smo.server(servername).information.rootdirectory + "\\data\\" 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -