如何获取备份和在C#中还原数据库

问题描述:



我在C#中有个爱好. &我正在使用SQL Server2005.我将数据库存储在bin \ debug \ App_Data文件夹中.

当我单击backup_button时,我希望获得数据库的支持.并在单击restore_button时将数据库还原到给定位置.

预先感谢.

Hi,

I have an appliaction in c#. & I am using SQL server 2005. I stored the database in bin\debug\App_Data folder.

I want to get the back of my database when I click on backup_button. & restore the database in given location when click on restore_button.

Thanks in advance.



尝试以下备份:

Hi,

Try this for backup:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SqlClient;
namespace Utilities
{
    public class Backup
    {
        public virtual bool MyFile(string targetSavePath, string catalogDb, string bConnToBackup)
        {
            //Author: Algem Mojedo
            bool retValue = false;
            DirectoryInfo dirInfo = new DirectoryInfo(targetSavePath);
            if (!dirInfo.Exists)
            {
                return retValue;
            }
            string todayIs = DateTime.Today.ToShortDateString().Trim().Replace("/", "-") + ".bak";
            StringBuilder sb = new StringBuilder();
            sb.Append(" USE MASTER " + "\r\n ");
            sb.Append(" DECLARE @fileName VARCHAR(50) " + "\r\n ");
            sb.Append(" DECLARE @fileDate VARCHAR(20)  " + "\r\n  ");
            sb.Append(" DECLARE @path VARCHAR(220)  " + "\r\n  ");
            sb.Append("\r\n ");
            sb.Append(" SET @path =  '" + targetSavePath + "'\r\n ");
            sb.Append(" SET @fileName = '" + catalogDb + "'\r\n ");
            sb.Append(" SELECT @fileDate = CONVERT(VARCHAR(20), GETDATE(),112) " + "\r\n "); //-- Fetching today's data
            sb.Append(" DECLARE db_cursor CURSOR FOR " + "\r\n ");
            sb.Append(" SELECT name FROM dbo.sysdatabases WHERE name IN ('" + catalogDb + "') " + "\r\n ");
            sb.Append(" OPEN db_cursor   FETCH NEXT FROM db_cursor INTO @fileDate " + "\r\n ");
            sb.Append(" WHILE @@FETCH_STATUS = 0   BEGIN  " + "\r\n ");
            sb.Append(" SET @fileName = @path + @fileDate + '_" + todayIs + "' \r\n ");
            sb.Append(" BACKUP DATABASE @fileDate TO DISK = @fileName " + "\r\n ");
            sb.Append(" FETCH NEXT FROM db_cursor INTO @fileDate  END " + "\r\n ");
            sb.Append(" CLOSE db_cursor   DEALLOCATE db_cursor " + "\r\n ");
            string sbStr = sb.ToString();
            string adCon = string.Empty;
            if (!(bConnToBackup.ToLower().Contains("timeout")))
            {
                adCon = ";Connect Timeout=300; pooling='true'; Max Pool Size=300";
            }
            SqlConnection conn = new SqlConnection((bConnToBackup + adCon));
            SqlCommand cmd = new SqlCommand(sbStr);
            try
            {
                cmd.Connection = conn;
                cmd.Connection.Open();
                cmd.CommandTimeout = 300;
                cmd.ExecuteScalar();
                retValue = true;
            }
            catch (Exception)
            {
                retValue = false;
                return retValue;
            }
            finally
            {
                cmd.Dispose();
                conn.Close();
            }
            return retValue;
        }
    }
}



如果有帮助,请记住对此进行投票...

问候,

Algem



Remember to vote on this if help...

Regards,

Algem


查看此链接可能对您有所帮助

使用C#和.NET 2.0的SQL Server 2005数据库备份和还原 [
view this link this might may help u

SQL Server 2005 Database Backup and Restore using C# and .NET 2.0[^]


请参阅以下链接

http://www.dotnetspider.com/forum/162986-database-backup- restore-through-C.aspx [ ^ ]

从下面的链接中可以获得示例代码....

http://midnightprogrammer.net/post/BackupRestore-SQL-database-using-C.aspx [^ ]
refer these links

http://www.dotnetspider.com/forum/162986-database-backup-restore-through-C.aspx[^]

from the link below you got sample code....

http://midnightprogrammer.net/post/BackupRestore-SQL-database-using-C.aspx[^]