Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
Hello all
i have a database sql in my project in c#.
please help me about backup and restore database.
(using linq is better)
thanks
Posted
Comments
[no name] 11-Apr-13 20:17pm    
The same way? http://www.codeproject.com/Questions/486320/Backupplusdatabseplusinplusc-23
FM7 11-Apr-13 20:31pm    
Not work.
Also I mean using LINQ if possible.Thanks
[no name] 11-Apr-13 23:00pm    
"Not work" is an unacceptable description of a problem. Do you call your mechanic on the phone and tell him "car broke" and expect he knows exactly what you mean? It worked well enough for you to accept that as the solution so what has changed? What is it that you think LINQ would do for you in a database backup and restore situation since LINQ is for querying data and has nothing to do with database backup and restore operations?
FM7 12-Apr-13 14:48pm    
thanks

Try This Code

Using System.Data.SqlClient Objcet

try
    {
        string command = @"BACKUP DATABASE DBDemo TO DISK='"+ strFileName+"'";
        SqlCommand oCommand = null;
        SqlConnection oConnection = null;
        oConnection = new SqlConnection("Data Source=MYPC/SQLEXPRESS;Initial Catalog=DBDemo;Integrated Security=True");
        if (oConnection.State != ConnectionState.Open)
        oConnection.Open();
        oCommand = new SqlCommand(command, oConnection);
        oCommand.ExecuteNonQuery();


    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
 
Share this answer
 
Comments
FM7 12-Apr-13 14:50pm    
thanks
You shouldn't backup your database from application code. That's just plain bad practice.

What you should do insted is do it manually from SSMS or create a scheduled job that will backup database every day or every week or whatever.

If there is a need to backup your database from application code that probably means something is not correct with your application logic.

Unless you are building database backup software ;)
 
Share this answer
 
Comments
FM7 12-Apr-13 14:52pm    
"do it manually from SSMS or create a scheduled job that will backup database every day or every week or whatever."
good advice.how can i do that my friend?
RobCollins 12-Apr-13 16:45pm    
From SSMS: Right click on the database -> Tasks -> Back up...

Scheduled: Connect to SQL Server instance from SSSM and then go to -> Management -> Maintenance plans -> Right click + new maintenance plan -> Name it "Daily backup" or something similar. -> From toolbox menu choose "Back Up Database Task"
FM7 13-Apr-13 17:49pm    
thanks my friend,is it possible to do that in c# application for my project?
my project with attachDB ,use SQL Express 2008 in VS2010
RobCollins 15-Apr-13 6:03am    
Sure - check out what rmksiva posted and try to build on top of that. Also, make sure your account has enough privileges to create and restore bacups.

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