1

Here is my code to back up a database.
These are my databases:
enter image description here

I got this as Error:

Database 'BakupDB' does not exist. Make sure that the name is entered correctly. BACKUP DATABASE is terminating abnormally.

And the code for my web page:

public partial class Default2 : System.Web.UI.Page
{
    string dbname = "BakupDB";
    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|\BakupDB.mdf;Integrated Security=True;User Instance=True";

        //Enter destination directory where backup file stored
        string destdir = "D:\\backupdb";

        try
        {
            sqlcon.Open();
            sqlcmd = new SqlCommand("backup database "+dbname+" to disk='" + destdir + "\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'", sqlcon);
            sqlcmd.ExecuteNonQuery();

            sqlcon.Close();
            Response.Write("Backup database successfully");
        }
        catch (Exception ex)
        {
            Response.Write("Error During backup database!");
        }
    }
}

What do I wrong?

Mike Guthrie
  • 4,029
  • 2
  • 25
  • 48
Will_G
  • 269
  • 2
  • 5
  • 18
  • Where are you getting the error you mentioned above? Is that the message of your caught exception? Have you checked that your `|DataDirectory|` is the expected path containing your .mdf files? – Mike Guthrie Mar 21 '13 at 17:23

2 Answers2

1

It looks like you're trying to use a User Instance. As an aside (not 100% related to your question) I believe that this feature is deprecated in SQL Server Express 2012 (and have been on a deprecation path since SQL Server 2008).

Sorry for the aside. With regards to your question perhaps the following will help you:

I would say the Stack Overflow question is closer to the use case you're trying to achieve with executing the SQL command, but have referenced the SMO way in case you were interested or wanted to try a different approach. Based on the SO question it appears that your connection string:

sqlcon.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\BakupDB.mdf;Integrated Security=True;User Instance=True";

Is missing a Database= section so perhaps it should read like:

sqlcon.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\BakupDB.mdf;Integrated Security=True;User Instance=True;Database=BakupDB";
Community
  • 1
  • 1
nkvu
  • 5,551
  • 2
  • 16
  • 12
0

This code worked for me:

private void button5_Click(object sender, EventArgs e)
{
    try
    {
        string dlink= @"DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\jsdb.mdf; Database=jsdb; User Instance=True; Integrated Security=True;Asynchronous Processing= True";
        SqlConnection dcc = new SqlConnection(dlink);
        dcc.Open();
            string database = "jsdb"; 
            string blc = textBox1.Text;  //(Its the location to save the file)
            string nm = "dbBackup";
            string dt =DateTime.Today.ToShortDateString();
            string sl = "BACKUP DATABASE "+database+" TO DISK = '"+blc+"\\"+nm+"-"+dt+".bak'" ;
            SqlCommand cql = new SqlCommand(sl,dcc);
            cql.ExecuteNonQuery();
            MessageBox.Show("Database backup completed successfully..");
            dcc.Close();
    } 
   catch (Exception ex)
    { MessageBox.Show(ex.Message); }
}
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Jaydeep_GAME
  • 61
  • 1
  • 1