我用的是VS2005 C#.NET,数据库是VS自带的SQLEXPRESS 2005。需要加入数据库备份还原的功能,可是出现“在 sysdatabases 中找不到数据库stu_ljl”的错误。我想备份的是App_Data下的stu_ljl.mdf
下面是完整的代码:
<div><asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="备份" /><br />
        <br />
        <br />
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList><br />
        <br />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="还原" />
        <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="删除" /></div>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;public partial class Admin_databr_br : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindFileName();
        }    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string sql = "Backup Database stu_ljl to DISK='" + Server.MapPath("~//Admin//BackUp//pubs" + DateTime.Now.ToString("yyyyMMddhhmmss")) + "'";
        SqlConnection pubsConn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True");
        SqlCommand cmd = new SqlCommand(sql, pubsConn);
        try
        {
            pubsConn.Open();
            int a = cmd.ExecuteNonQuery();
            if (a == -1)
            {
                Response.Write("success!");
            }
            else
            {
                Response.Write("fail!");
            }
        }
        catch(SqlException ex)
        {
            Response.Write(ex.ToString());
        }
        finally
        {
            pubsConn.Close();
            BindFileName();
        }
    }    public void BindFileName()
    {
        DropDownList1.Items.Clear();
        string path = Server.MapPath("~//Admin//BackUp");
        DirectoryInfo di = new DirectoryInfo(path);
        foreach (FileInfo fi in di.GetFiles())
        {
            ListItem li = new ListItem();
            li.Value = fi.FullName;
            li.Text = fi.Name;
            DropDownList1.Items.Add(li);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string sql = "Restore Database stu_ljl From DISK='" + DropDownList1.SelectedValue + "'";
        SqlConnection masConn = new SqlConnection(ConfigurationManager.ConnectionStrings["contoadmin"].ToString());
        SqlCommand cmd = new SqlCommand(sql, masConn);
        masConn.Open();
        int a = cmd.ExecuteNonQuery();
        if (a == -1)
        {
            Response.Write("success!");
        }
        else
        {
            Response.Write("fail!");
        }
        masConn.Close();    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        File.Delete(DropDownList1.SelectedValue);
        BindFileName();
    }
}

解决方案 »

  1.   

    如果是备份 sqlserver的话  
    备份:::
    public static void SQLBACK(string ServerIP,string LoginName,string LoginPass,string DBName,string BackPath)
            {
                SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
                SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
                try
                {
                    oSQLServer.LoginSecure = false;
                    oSQLServer.Connect(ServerIP, LoginName, LoginPass);
                    oBackup.Database = DBName;
                    oBackup.Files = BackPath;
                    oBackup.BackupSetName = DBName;
                    oBackup.BackupSetDescription = "数据库备份";
                    oBackup.Initialize = true;
                    oBackup.SQLBackup(oSQLServer);
                }
                catch (Exception e)
                {
                throw new Exception(e.ToString());
                }
                finally
                {
                oSQLServer.DisConnect();
                }
            }
      

  2.   

    用了3楼的还是提示 “在 sysdatabases 中找不到数据库stu_ljl 所对应的条目”,我觉得是我这边什么地方设置的问题,可我只安装了sql express 2005啊,就是VS2005自带的那个。