日期:2014-05-18  浏览次数:20439 次

如何用asp.net(c#)代码实现数据库,表的备份与恢复
求代码

------解决方案--------------------
参考
------解决方案--------------------
表的备份,关注,如果是数据库的备份还略知一二,帮顶
不过备份库的同时也备份了表,
给个SQL语句,
C# code
//备份
    public bool DBBack(string path, string DBName)
    {
        SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
        string sql = "use master;";
        sql += "backup database @dbName to disk=@path;";
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.Parameters.Add("@dbName", SqlDbType.Char);
        cmd.Parameters["@dbName"].Value = DBName;
        cmd.Parameters.Add("@path", SqlDbType.Char);
        cmd.Parameters["@path"].Value = DBName;
        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        catch(Exception ex)
        {
            conn.Close();
            return false;
        }
        return true;
    }
    //还原
    public bool DBRestore(string path, string DBName)
    {
        SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
        string sql = "use master;";
        sql += "restore database @dbName from disk=@path;";
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.Parameters.Add("@dbName", SqlDbType.Char);
        cmd.Parameters["@dbName"].Value = DBName;
        cmd.Parameters.Add("@path", SqlDbType.Char);
        cmd.Parameters["@path"].Value = DBName;
        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        catch (Exception ex)
        {
            conn.Close();
            return false;
        }
        return true;
    }