SQL Server数据库还原和备份 [英] SQL Server Database restore and backup

查看:73
本文介绍了SQL Server数据库还原和备份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

这里正在共享SQL Server 2008的备份和恢复代码。我可以进行备份,但是我无法确定是否正在处理还原操作,请分享关于代码的评论。

和备份代码:

Hi friends,
Here am sharing the backup and restore code for SQL Server 2008.In that I can able to make backup but I cant able to identify whether restore operation is handling or not,Please share the comments about the code.
And the code for backup:

private void btnBackup_Click(object sender, EventArgs e)
        {
             if (cmbDatabase.Text.CompareTo("") == 0)
            {
                MessageBox.Show("Please Select Database");
                return;
            }
            try
            {
                con = new SqlConnection(connectionString);
                con.Open();
                string sql = "BACKUP DATABASE " + cmbDatabase.Text + " TO DISK = '" + txtDBBackup.Text + "\\" + cmbDatabase.Text + "-" + DateTime.Now.Ticks.ToString() + ".bak'";
                cmd = new SqlCommand(sql, con);
                cmd.ExecuteNonQuery();
                txtDBBackup.Text = "";
                MessageBox.Show("Successfully Backed Up.");
                txtDBBackup.Text = string.Empty;
                con.Close();
                con.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }



恢复代码为:


And the code for restore is:

private void btnRestore_Click(object sender, EventArgs e)
      {
          try
          {
              if (cmbDatabase.Text.CompareTo("") == 0)
              {
                  MessageBox.Show("Please select a Database.");
                  return;
              }
              con = new SqlConnection(connectionString);
              con.Open();
              sql = "Alter Database " + cmbDatabase.Text + " Set SINGLE_USER WITH ROLLBACK IMMEDIATE;";
              sql += "Restore Database " + cmbDatabase.Text + " FROM Disk = '" + txtDBRestore.Text + "' WITH REPLACE;";
              cmd = new SqlCommand(sql, con);
              cmd.ExecuteNonQuery();
              con.Close();
              con.Dispose();
              MessageBox.Show("Successfully Restore Database");
              txtDBRestore.Text = string.Empty;
              cmbDatabase.Text = string.Empty;
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }

推荐答案

(1)查看教程



http://www.youtube.com/watch?v=K2C_ia0KGCc& feature = youtu.be



(2)试用代码

使用我上面提到的免费控制和njoy数据库操作使用更少的代码

(1) View the Tutorial

http://www.youtube.com/watch?v=K2C_ia0KGCc&feature=youtu.be

(2) Try out the code
use my above mentioned free control and njoy Database operations with less code
dcon.ExecNonQuery("delete from folderPath");
dcon.ExecNonQuery("BACKUP DATABASE Timber_Demo TO  DISK = N'" + txtFName.Text + "'");
MessageBox.Show("Backup Done Successfully");



//恢复操作


//Restore Operation

txtFName.Text = openFileDialog1.FileName;
                   dcc = new DBConClass();
                   // dcc.setSQLConnection(@".\sqlexpress", "master", "sa", "sql");
                   dcc.setSQLConnection(@".", "master", "sa", "sql");
                   string alt = "ALTER DATABASE Timber_Demo SET OFFLINE WITH ROLLBACK IMMEDIATE";
                   dcc.ExecNonQuery(alt);

                   dcc = new DBConClass();
                   // dcc.setSQLConnection(@".\sqlexpress", "master", "sa", "sql");
                   dcc.setSQLConnection(@".", "master", "sa", "sql");
                   string restore = "restore database Timber_Demo from DISK =N'" + txtFName.Text + "'WITH FILE=1,NOUNLOAD,REPLACE,Stats=10";
                   int res = dcc.ExecNonQuery(restore);


                   dcc = new DBConClass();
                   //  dcc.setSQLConnection(@".\sqlexpress", "master", "sa", "sql");
                   dcc.setSQLConnection(@".", "master", "sa", "sql");
                   alt = "ALTER DATABASE Timber_Demo SET ONLINE";
                   dcc.ExecNonQuery(alt);

                   MessageBox.Show("Data Restored successfully");

                   txtFName.Text = "";
                   // dcc.setSQLConnection(@".\sqlexpress", "DBStore", "sa", "sql");
                   dcc.setSQLConnection(@".", "Timber_Demo", "sa", "sql");


请阅读以下链接以恢复sql server数据库。



http://stackoverflow.com/questions/ 962441 / microsoft-sql-server-restore-a-backup-of-a-database-with-one-command [ ^ ]
please read bellow links for restoring sql server database.

http://stackoverflow.com/questions/962441/microsoft-sql-server-restore-a-backup-of-a-database-with-one-command[^]


这篇关于SQL Server数据库还原和备份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆