How Can I Obtain The Default Backup Path For Sql Server 2008 Express R2 Programmatically Using C#?
Solution 1:
To retrieve the Backup path for a given server you could use the SQL Server Management Objects. The Server object has a property called BackupDirectory.
You'll want something like this:
Serversrv=newServer("SERVERNAME");
stringbackUpDir= srv.BackupDirectory;
For this to work you will need to import usings/references for:
Microsoft.SqlServer.Management.Smo;
Microsoft.SqlServer.Management.Common;
You will find more information here on how to interact with the SQL Server Management Objects.
Solution 2:
Try to adopt this stub:
privatevoidGetSqlDefaultInfo(string InstanceName, string ServerName)
{
try
{
InstanceName = string.IsNullOrEmpty(InstanceName) ? "MSSQLSERVER" : InstanceName;
if (string.IsNullOrEmpty(ServerName))
ServerName = Environment.MachineName;
using (var registryKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, ServerName))
{
object sqlInstance;
using (var subKey = registryKey.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL"))
sqlInstance = subKey.GetValue(InstanceName);
if (sqlInstance != null && !string.IsNullOrEmpty(sqlInstance.ToString()))
{
var sqlPathKey = string.Format(@"SOFTWARE\Microsoft\Microsoft SQL Server\{0}\MSSQLServer",
sqlInstance);
object defaultData, defaultLog, backupDirectory, sqlPath;
using (var subKey = registryKey.OpenSubKey(sqlPathKey))
{
defaultData = subKey.GetValue("DefaultData");
defaultLog = subKey.GetValue("DefaultLog");
backupDirectory = subKey.GetValue("BackupDirectory");
}
sqlPathKey = string.Format(@"SOFTWARE\Microsoft\Microsoft SQL Server\{0}\Setup", sqlInstance);
using (var subKey = registryKey.OpenSubKey(sqlPathKey))
sqlPath = subKey.GetValue("SQLDataRoot");
DataFilePath = defaultData != null
? defaultData.ToString()
: Path.Combine(sqlPath.ToString(), "Data").TrimEnd('\\');
LogFilePath = defaultLog != null
? defaultLog.ToString()
: Path.Combine(sqlPath.ToString(), "Data").TrimEnd('\\');
FTSIndexFilePath = DataFilePath;
ContentFilePath = DataFilePath;
BackupFilePath = backupDirectory != null
? backupDirectory.ToString()
: Path.Combine(sqlPath.ToString(), "Backup").TrimEnd('\\');
}
}
} catch(Exception)
{
}
}
Solution 3:
The following query should give you the physical device name (or path) for each database. You can tailor it to suit your needs:
select
database_name,
backup_type,
physical_device_name -- pathfrom
(
selectrow_number() over (partitionby database_name,type orderby backup_start_date desc) as rownum,
database_name,
case type
when'L'then'Log'when'D'then'Data'else'??? '+type
endas backup_type,
physical_device_name
from msdb.dbo.backupset a
join msdb..backupmediaset b on a.media_set_id = b.media_set_id
join msdb.dbo.backupmediafamily c on a.media_set_id = c.media_set_id
) x
where rownum=1orderby database_name asc, backup_type
Solution 4:
I ended up using the following SqlCommand ... This solution appeared in another answer that was for some reason erased.? Anyway, here is the command I ended up using:
EXEC master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'Software\\Microsoft\\MSSQLServer\\MSSQLServer',N'BackupDirectory'And here is the command in my C# code which shows the default backup folder path getting stored in the variable backupFolder:
SqlConnectionmyConnection=newSqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);
SqlCommandcmd=newSqlCommand();
SqlDataReader reader;
cmd.CommandText = "EXEC master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'Software\\Microsoft\\MSSQLServer\\MSSQLServer',N'BackupDirectory'";
cmd.CommandType = CommandType.Text;
cmd.Connection = myConnection;
myConnection.Open();
SqlDataReadermyDataReader= cmd.ExecuteReader();
myDataReader.Read();
stringbackupFolder= myDataReader.GetString(1);
Solution 5:
The value you're looking for is stored in the registry
Key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.2\MSSQLServerValue: BackupDirectoryNote that the key depends on your SQL Server instance name.
Post a Comment for "How Can I Obtain The Default Backup Path For Sql Server 2008 Express R2 Programmatically Using C#?"