Skip to content

C# Program Update or SQL EXPRES Database Backup

Jasmin M

Asked Mar 5, 2014, 10:49 AM EST3 answers
Share
LinkedInX
Hello, i created a program in Visual Basic 2012 with Internal database ( Service Based Database)- which im going to use as a Local database, i deployed it and installed in a client PC,now  he wants me to make some changes in the program, but doesnt wants to lose the data he inserted in the program.
How can i backup and restore the data or how can i Update the Program with the changes i made.
ps. i installed : SQL Expres and SQL Local DB in clients pc

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    For Backup

    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=your_database_name;Integrated Security=True");
                con.Open();
                try
                {
                    SqlCommand cmd = new SqlCommand("backup database email_client to disk ='" + path+ "\\" + name_you_want_to_give_to_database + ".bak'", con);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("done");
     }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
    Div
     For Restore

     SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
                con.Open();
                try
                {


                    sql = "alter database your_database_name set single_user with rollback immediate ;";
                    sql += "RESTORE database your_database_name from disk='"+backup_file_name+"'with replace;";
                    cmd = new SqlCommand(sql, con);
                    cmd.ExecuteNonQuery();
                    con.Close();
                    con.Dispose();
                    MessageBox.Show("done");


                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
    0
  • 0
    Hey thanks a lot for these codes, it seems like this is what im loking for, but i get in an error :
    it says this message : 
                                    Database 'TEST' does not exist. Make sure that the name is entered correctly. Backup Database is terminating abnormally.

    I put the right name in the codes but i dont know why cant i backup it (i think it might be that i use an internal local database). Pls help me solve this
    0
  • 1 short reply
    • 0
      send me your whole code
      0