Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, January 25, 2013 1:07 PM
i have have 2 buttons with names Backup and restore.
when i click the button Backup.. i should see options where to backup then after i select where i backup..then hit
backup button. the database of my application should be backed up.
can you show me on how to make both the backup and restore database in windows application using c#?
thanks all hope to see the answer..
All replies (4)
Friday, January 25, 2013 1:32 PM ✅Answered
Hi,
What kind of database are you using? For localDB or Access, copying the file is enough. For SQL you should do something more. For Oracle.... and so on ;)
Hope this helps
Please mark the best replies as answers
Blog: bloggingabout.net/blogs/rick
Twitter: @rickvdbosch
Friday, January 25, 2013 5:39 PM ✅Answered
As I understand it:
You have an application with a Database.
You want to have an way of saving/restoring said database into files - similar to how you save a word document using using save/save as and open menu options.
If that is correct:
How many tables to you have and how do they look?
What amount of data do you have?
Wich UI Framework do you use?
Dependign on wich UI Framework you have, there should be a OpenFile and SaveFile Dialog avalible (they are among the most commonly used), whose sole purpose is to find the path/filename in a userfriendly way.
Friday, January 25, 2013 5:40 PM ✅Answered
Hi,
Same kind of problem is discussed in below
Database backup and restore with C#.net
Sunday, January 27, 2013 4:38 AM ✅Answered
Microsoft.SqlServer.Smo
Microsoft.SqlServer.SmoExtended
Microsoft.SqlServer.ConnectionInfo
Microsoft.SqlServer.Management.Sdk.Sfc
Microsoft.SqlServer.SqlEnum
string ServerName = "server";
string DatabaseName = database;
string UserId = userId;
string Password = password;
ServerConnection serverConnection;
private Server _server;
public Server Server
{
get
{
if (_server == null)
{
Connect();
}
return _server;
}
}
string connectionString
{
get
{
return string.Format("Data Source={0};Initial Catalog={1};UID={2};Password={3}", ServerName, DatabaseName, UserId, Password);
}
}
public void Connect()
{
_serverConnection = new ServerConnection();
_serverConnection.LoginSecure = false;
_serverConnection.Login = UserId;
_serverConnection.Password = Password;
_server = new Server(_serverConnection);
}
public void BackupDatabase()
{
using (SqlConnection cn = new SqlConnection(connectionString))
{
ServerConnection svCon = new ServerConnection(cn);
Server svr = new Server(svCon);
cn.Open();
cn.ChangeDatabase("master");
string testFolder = @"C:\temp";
string databaseName = _databaseName;
Backup backup = new Backup();
backup.Action = BackupActionType.Database;
backup.Database = databaseName;
backup.Incremental = false;
backup.Initialize = true;
backup.LogTruncation = BackupTruncateLogType.Truncate;
string fileName = string.Format("{0}\\{1}.bak", testFolder, databaseName);
BackupDeviceItem backupItemDevice = new BackupDeviceItem(fileName, DeviceType.File);
backup.Devices.Add(backupItemDevice);
backup.PercentCompleteNotification = 10;
//backup.PercentComplete += backup_PercentComplete;
//backup.Complete += backup_Complete;
backup.SqlBackup(svr);
if (!VerifyBackup(svr))
{
//throw new Exception("Backup could not be verified.");
}
svr = null;
cn.Close();
}
}
public bool VerifyBackup(Server svr)
{
string testFolder = @"C:\temp";
string databaseName = _databaseName;
Restore restore = new Restore();
restore.Action = RestoreActionType.Database;
string fileName = string.Format("{0}\\{1}.bak", testFolder, databaseName);
BackupDeviceItem backupItemDevice = new BackupDeviceItem(fileName, DeviceType.File);
restore.Devices.AddDevice(fileName, DeviceType.File);
restore.Database = databaseName;
restore.PercentCompleteNotification = 10;
//restore.PercentComplete += new PercentCompleteEventHandler(ProgressEventHandler);
bool verified = restore.SqlVerify(svr);
return verified;
}
public void RestoreDatabase()
{
using (SqlConnection cn = new SqlConnection(connectionString))
{
ServerConnection svCon = new ServerConnection(cn);
Server svr = new Server(svCon);
cn.Open();
cn.ChangeDatabase("master");
string testFolder = @"C:\temp";
string databaseName = _databaseName;
Restore restore = new Restore();
restore.Action = RestoreActionType.Database;
string fileName = string.Format("{0}\\{1}.bak", testFolder, databaseName);
BackupDeviceItem backupItemDevice = new BackupDeviceItem(fileName, DeviceType.File);
restore.Devices.AddDevice(fileName, DeviceType.File);
restore.Database = databaseName;
restore.ReplaceDatabase = true;
restore.PercentCompleteNotification = 10;
//restore.PercentComplete += new PercentCompleteEventHandler(ProgressEventHandler);
svr.KillAllProcesses(databaseName);
restore.SqlRestore(svr);
svr = null;
cn.Close();
}
}