65.9K
CodeProject 正在变化。 阅读更多。
Home

SQL Server基于Web的备份工具

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.41/5 (7投票s)

2016年3月18日

CPOL

2分钟阅读

viewsIcon

23759

downloadIcon

385

一个用于从 MS-SQL Server 获取备份的 ASP.NET 单页应用程序

引言

要从 Microsoft SQL Server 数据库获取备份,通常需要使用 SSMS(SQL Server Management Studio)。这对于普通用户来说不是一个合适的解决方案。普通用户不具备使用 SSMS 的知识,并且还存在其他问题,例如端口限制等。

我认为一个功能完善的应用程序应该具有自己的数据库管理工具,例如备份工具。

在我的最后一个项目中,我将备份工具添加到我的 Web 应用程序中,并想与您分享我的代码。

背景

这是我的应用程序的最新版本,第一个版本是在 ASP.NET Web Form 上运行的,但我扩展了它,当前版本在 Ajax 和 ASP.NET Web Service 上运行。

Using the Code

要使用此代码,您应该将 5 个程序集文件添加到您的 Web 项目中

  • Microsoft.SqlServer.ConnectionInfo
  • Microsoft.SqlServer.Management.Sdk.Sfc
  • Microsoft.SqlServer.Smo
  • Microsoft.SqlServer.SmoExtended
  • Microsoft.SqlServer.SqlEnum

如您所见,该应用程序有一个主要的 Web 服务“BackupServices.asmx”和一个 html 文件“index.html”。

Web 服务具有 2 个 Web 方法

[WebMethod(enableSession: true)]
public NSResult GetDatabases(string user, string password, string server)
{
    NSResult result = new NSResult();
    ServerConnection cc = new ServerConnection();

    if (GetServerConnection(user, password, server, ref cc))
    {
        result.LoginResult = "1";
        result.DatabaseList = GetDatabaseList(cc);
    }
    else
    {
        result.LoginResult = "0";
    }
    return result;
}

此 Web 方法用于检查用户的身份验证,如果身份验证正确,则返回实例的数据库列表。此 Web 方法由 JavaScript 函数调用

function LoginToSql() {
    $user = $("#txtUsername").val();
    $pass = $("#txtPassword").val();
    $server = $("#txtServer").val();

    $.ajax({
        type: "POST",
        url: "BackupServices.asmx/GetDatabases",
        data: "{ 'user': '" + $user + "', 'password' : 
        '" + $pass + "', 'server' : '" + $server + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        beforeSend: function () {
            $('#loader').fadeIn("slow");
        },
        success: function (msg) {
            if (msg.d.LoginResult == "1") {
                $("#dvLoginResult").html("Login Succeed!");
                $("#dvLoginResult").removeClass("btn-danger")
                $("#dvLoginResult").addClass("btn-success")
                $("#dvLoginResult").fadeIn("slow");
                $("#authentication-info").attr("disabled","disabled");
                setTimeout("$('#dvLoginResult').fadeOut('slow')", 1000);

                $("#drpDbList").html(msg.d.DatabaseList);
                $("#backup-form").fadeIn("slow");
            }
            else {
                $("#dvLoginResult").html("Login Failed");
                $("#dvLoginResult").removeClass("btn-success")
                $("#dvLoginResult").addClass("btn-danger")

                $("#dvLoginResult").fadeIn("slow");
                setTimeout("$('#dvLoginResult').fadeOut('slow')", 1000);
                $("#backup-form").fadeOut("slow");
            }
        },
        complete: function () {
            $('#loader').fadeOut("slow");
        }
    });
}

如您所见,此函数通过 Ajax 函数连接到 Web 方法。在成功回调函数中,它检查结果,如果身份验证正确,则将数据库列表添加到页面中的下拉列表中。

true条件下,此函数执行另外两个必要的工作

  • 禁用身份验证表单
  • 显示备份表单

备份过程中使用的第二个 Web 方法是

[WebMethod(enableSession: true)]
public NSResult GetDatabases(string user, string password, string server)
{
    NSResult result = new NSResult();
    ServerConnection cc = new ServerConnection();

    if (GetServerConnection(user, password, server, ref cc))
    {
        result.LoginResult = "1";
        result.DatabaseList = GetDatabaseList(cc);
    }
    else
    {
        result.LoginResult = "0";
    }
    return result;
}

此 Web 方法由 BackupSql() JavaScript 函数调用

function BackupSql() {
    $user = $("#txtUsername").val();
    $pass = $("#txtPassword").val();
    $server = $("#txtServer").val();
    $dbName = $("#drpDbList").val();``
    $FileName = $("#txtFileName").val();

    $.ajax({
        type: "POST",
        url: "BackupServices.asmx/BackupDatabase",
        data: "{ 'user': '" + $user + "', 'password' : '" + $pass + "', 
	'server' : '" + $server + "', 'dbName' : '" + $dbName + "', 
	'fileName' : '" + $FileName + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        beforeSend: function () {
            $('#loader').fadeIn("slow");
        },
        success: function (msg) {
            if (msg.d.LoginResult == "1") {
                $("#dvBackupResult").html(msg.d.ReturnMessage);
                $("#dvBackupResult").removeClass("btn-danger")
                $("#dvBackupResult").addClass("btn-success")
                $("#dvBackupResult").fadeIn("slow");
                setTimeout("$('#dvBackupResult').fadeOut('slow')", 8000);
            }
            else {
                $("#dvBackupResult").html(msg.d.ReturnMessage);
                $("#dvBackupResult").removeClass("btn-success")
                $("#dvBackupResult").addClass("btn-danger")

                $("#dvBackupResult").fadeIn("slow");
                setTimeout("$('#dvBackupResult').fadeOut('slow')", 8000);
            }
        },
        complete: function () {
            $('#loader').fadeOut("slow");
        }
    });
    }

我还将两个简单的类添加到我的 Web 应用程序中,您可以在 App_Code 中看到它们。这些用作 Json 对象。

关注点

在这个简单的代码中,我对命名空间 (Microsoft.SqlServer.Management.Smo) 更加熟悉,它非常有用。

© . All rights reserved.