Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
How can i create the backup of sql database by c# code. Any sample code.

I am using this command to backup,it' snot working.

cmd.CommandText = @"backup database tryDatabase.mdf to disk ='d:\BackUp1.bak' with init,stats=10 ";
Posted

Have a look here: Backing up an SQL Database in C#[^]
 
Share this answer
 
Comments
footballpardeep 1-Oct-12 15:34pm    
Thanks for reply, i don't want to use any dll for this. i wanna do it by sql command.
footballpardeep 1-Oct-12 16:03pm    
I was not using initial catalog in connection string.
when i used this:
string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\PARDEEP\Documents\Visual Studio 2010\Projects\TryingDatabaseInWPF\TryingDatabaseInWPF\tryDatabase.mdf;Initial Catalog=tryDatabase;Integrated Security=True;User Instance=True";

It worked.
footballpardeep 1-Oct-12 16:04pm    
But now the problem is how can i restore the database.
Hi footballpardeep,

Why you didnt ask google about it?
I hope this link[^] would help you a bit.

Regards,
RK
 
Share this answer
 
v2
1 Back up

OpenFileDialog ofd = new OpenFileDialog();
SaveFileDialog sfd = new SaveFileDialog();
CashFlowDAL cfd = new CashFlowDAL();
string filepath;
if (!Directory.Exists("C:\\"))
{
filepath = "C:\\BACKUP\\" + User.compname;
}
else
{
filepath = "E:\\BACKUP\\" + User.compname;
}
string res = "";

{
if (!Directory.Exists(filepath))
{
Directory.CreateDirectory(filepath);
}
try
{

string filename = DateTime.Now.ToString(User.compname + " dd_MM_yy__hh_mm_ss.bak");
res = cfd.BackupDatabase(User.compname, filepath + "\\" + filename);
if (res != null) throw new Exception(res);
File.Copy(filepath + "\\" + filename, User.backupdir + "\\" + filename);
MessageBox.Show("Backup Completed..(In " + filepath + "\\" + filename + ")");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}


2 Restore

try
{
CashFlowDAL cfd = new CashFlowDAL();
cfd.ConnectToDB();
OpenFileDialog od = new OpenFileDialog();
od.Filter = "SQL Server database Restore files|*.bak";
od.Title = "Restore Database Backup";
if (od.ShowDialog() == DialogResult.OK)
{
this.Cursor = Cursors.WaitCursor;

string UseMaster = "USE master";
SqlCommand UseMasterCommand = new SqlCommand(UseMaster, cfd.con);
UseMasterCommand.ExecuteNonQuery();

string Alter1 = @"ALTER DATABASE [" + User.compname + "] SET Single_User WITH Rollback Immediate";
SqlCommand Alter1Cmd = new SqlCommand(Alter1, cfd.con);
Alter1Cmd.ExecuteNonQuery();

string Restore = string.Format("Restore database [" + User.compname + "] from disk='{0}'", od.FileName);

SqlCommand RestoreCmd = new SqlCommand(Restore, cfd.con);
RestoreCmd.ExecuteNonQuery();

string Alter2 = @"ALTER DATABASE [" + User.compname + "] SET Multi_User";
SqlCommand Alter2Cmd = new SqlCommand(Alter2, cfd.con);
Alter2Cmd.ExecuteNonQuery();
MessageBox.Show("Database Retored Sucessfully", "Success Message!");
}
}
catch (Exception)
{
MessageBox.Show("Database didn't Restore", "Error Message!");
}
this.Cursor = Cursors.Default;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900