Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi.

i am a beginner programmer.

i want to back up and restore database in my expreproject in sqlexpress visualstudio2005

in this code i getting sqlserver and sqlservername

C#
string servername;
            string sConnect = global::WindowsApplication16.Properties.Settings.Default.alakiConnectionString;
            string dbname;
            using (SqlConnection sc = new SqlConnection(sConnect))
            {
                sc.Open();
                dbname = sc.Database.ToString();
                ServerConnection sconn = new ServerConnection(sc);
                Server scv = new Server(sconn);
                servername= scv.Name.ToString();


and in this i want to back up of database;
C#
public void backupdatabase(string databasename, string servername, string destinationpath)
{
    Backup backup1 = new Backup();
    backup1.Action = BackupActionType.Database;
    backup1.BackupSetDescription = "Archivedatabase:" + DateTime.Now.ToShortDateString();
    backup1.BackupSetName = databasename;
    BackupDeviceItem deviceitem = new BackupDeviceItem(destinationpath, DeviceType.File);
    ServerConnection connection = new ServerConnection(servername);
    Server sqlserver = new Server(connection);
    Database db = sqlserver.Databases[databasename];
    backup1.Initialize = true;
    backup1.Checksum = true;
    backup1.ContinueAfterError = true;
    backup1.Devices.Add(deviceitem);
    backup1.Incremental = false;
    backup1.ExpirationDate = DateTime.Now.AddDays(3);
    backup1.LogTruncation = BackupTruncateLogType.Truncate;
    backup1.FormatMedia = false;

    backup1.SqlBackup(sqlserver);
}



but i encountered with this error:
back up failed for server..............

please help me
thanks.
Posted
Updated 20-Sep-11 23:10pm
v3
Comments
jim lahey 21-Sep-11 5:10am    
Added pre tags for readability

Use the following TSQL on a SqlConnection instead :

SQL
BACKUP DATABASE [databasename] TO DISK = 'c:\backup.bak'
 
Share this answer
 
hi thanks mehdi for your answer but i want learn this way because in your suggest way it is dificult to restore database
it is true for backup:
C#
string t = Convert.ToString(pf.Year) + "-" + Convert.ToString(pf.Month) + "-" + Convert.ToString(pf.Day);
                   conpa = path + "\\Backupofalaki" + t;
                   System.IO.Directory.CreateDirectory(conpa);
                   SqlConnection connect = new SqlConnection(con);
                   SqlCommand sc = new SqlCommand();
                   try
                   {
                      connect.Open();
                       textBox1.Text = connect.Database;
                       sc.CommandText ="BACKUP DATABASE[H:\\VISUAL STUDIO 2005\\PROJECTS\\WINDOWSAPPLICATION16\\WINDOWSAPPLICATION16\\BIN\\DEBUG\\bank.MDF] TO DISK = '" + conpa + "\\bank.MDF" + "'";
                       sc.Connection = connect;
                      sc.ExecuteNonQuery();
                      MessageBox.Show("success");
                   }
                   catch (Exception ex)
                  {
                      MessageBox.Show(ex.Message);
                    //textBox1.Text = ex.Message;
                     MessageBox.Show("problem");
                  }
                  finally
                  {
                      if (connect.State == ConnectionState.Open)
                          connect.Close();
                  }

but is is have error for restore
C#
 try
                {
                    string con = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\bank.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
                    SqlConnection connect = new SqlConnection(con);
                    SqlCommand cmd = new SqlCommand();
                    connect.Open();
                    string s1 = "ALTER DATABASE bank SET OFFLINE WITH ROLLBACK IMMEDIATE";
                    string s2 = "ALTER DATABASE bank SET MULTI_USER";
                    string query = "RESTORE DATABASE bank FROM DISK ='" + path + "'" + "WITH  FILE = 1,  KEEP_REPLICATION,  NOUNLOAD,  REPLACE,  STATS = 10";


                cmd.CommandText = s1;
                cmd.Connection = connect;
                cmd.ExecuteNonQuery();

                cmd.CommandText = s2;
                cmd.Connection = connect;
                cmd.ExecuteNonQuery();

                   cmd.CommandText = query;
                   cmd.Connection = connect;
                   cmd.ExecuteNonQuery();

                   connect.Close();
                  MessageBox.Show("restor success");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("errror");
                   MessageBox.Show(ex.Message);
}

i encountered with this exeption:
User does not have permission to alter database 'alaki' or the database does not exist.
ALTER DATABASE statement failed.
pleaze help
 
Share this answer
 
v3
Comments
André Kraak 21-Sep-11 8:59am    
If you have a question about or comment on the given solution use the "Have a Question or Comment?" option beneath the solution. When using this option the person who gave the solution gets an e-mail message and knows you placed a comment and can respond if he/she wants.

Please move the content of this solution to the solution you are commenting on and remove the solution. Thank you.
Change its connection string to master database and then try to restore it

string con = @"Data Source=.\SQLEXPRESS;Initial Catalog =master;Integrated Security=True;Connect Timeout=30;User Instance=True";



Restore to your existing database in SQL server
 
Share this answer
 
v3

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