Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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))
        {
            System.IO.Directory.CreateDirectory("D:\\backupdb");
        }
        try
        {
            //Open connection
            sqlcon.Open();
            //query to take backup database
            sqlcmd = new SqlCommand("backup database ASPNETDB to disk='" + destdir + "\\hallo1.Bak'", sqlcon);
            sqlcmd.ExecuteNonQuery();
            //Close connection
            sqlcon.Close();
            Response.Write("Backup database successfully");
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
            Response.Write("Error During backup database!");
        }
    }
}
share|improve this question
    
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. –  Shawn Melton 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
add comment

1 Answer 1

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 .

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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