I want to back up my database but I got this error:

Cannot open database "ASPNETDB" requested by the login. The login failed. Login failed for user 'My-laptop\user'.

I'm using SQL Server Express

This is the code how I back up my DB:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Default2 : System.Web.UI.Page
    //Metioned here your database name
    string dbname = "ASPNETDB";
    SqlConnection sqlcon = new SqlConnection();
    SqlCommand sqlcmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)

    protected void Button1_Click1(object sender, EventArgs e)
        //Mentioned Connection string make sure that user id and password sufficient previlages
        sqlcon.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.mdf;Integrated Security=True;User Instance=True;Database=ASPNETDB";
        //Enter destination directory where backup file stored
        string destdir = "D:\\backupdb";

        //Check that directory already there otherwise create 
        if (!System.IO.Directory.Exists(destdir))
            //Open connection
            //query to take backup database
            sqlcmd = new SqlCommand("backup database ASPNETDB to disk='" + destdir + "\\hallo1.Bak'", sqlcon);
            //Close connection
            Response.Write("Backup database successfully");
        catch (Exception ex)
            Label1.Text = ex.Message;
            Response.Write("Error During backup database!");
  • Right off I would say you don't have permission to login to your database. Other than that please provide how you are attempting to backup the database and what RDBMS you are using.
    – user507
    Mar 25 '13 at 8:44
  • That code seems to indicate you're using SQL Server Express, not MySQL. Please clarify by editing your post, and tell us whether you're able to log in to that database at all.
    – Mat
    Mar 25 '13 at 8:52
  • I create that DB with visual studio but don't have login data.?
    – Will_G
    Mar 25 '13 at 8:58

Well, the problem is: you cannot backup a SQL Server database that's used with the AttachDbFileName= property in the connection string:

Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.mdf;
Integrated Security=True;User Instance=True;Database=ASPNETDB";

If you want to use SMO to backup your database, the database must be attached to the SQL Server instance you're backing up from .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.