Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Back up database Error

Back up database Error

Scheduled Pinned Locked Moved Database
helpquestioncsharpdatabasevisual-studio
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sara setare
    wrote on last edited by
    #1

    Hi all,I have a problem with back up command! I use this code but it doesnt work:

    string constr = @"DataSource=.\SQLEXPRESS;AttachDbFilename=C:\Users\EHSAN\Documents\Visual Studio 2010\Projects\hokm\hokm\App_Data\Database1.mdf;Integrated Security=True;User Instance=True;";
    System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection ();
    con.ConnectionString = constr;
    con.Open();
    System.Data.SqlClient.SqlCommand comm=newSystem.Data.SqlClient.SqlCommand();
    comm.CommandText = @"backup database Database1 to disk ='c:\Users\EHSAN\Documents\Visual Studio 2010\Projects\hokm\hokm\DBbackup.bak' with init,stats=10";
    comm.Connection = con;
    comm.ExecuteNonQuery();
    con.Close();

    and when I run it, I get this error: Database 'Database1' does not exist. Make sure that the name is entered correctly. BACKUP DATABASE is terminating abnormally. but I use the database1 and I connect it successfully! what is the problem?

    M L 2 Replies Last reply
    0
    • S sara setare

      Hi all,I have a problem with back up command! I use this code but it doesnt work:

      string constr = @"DataSource=.\SQLEXPRESS;AttachDbFilename=C:\Users\EHSAN\Documents\Visual Studio 2010\Projects\hokm\hokm\App_Data\Database1.mdf;Integrated Security=True;User Instance=True;";
      System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection ();
      con.ConnectionString = constr;
      con.Open();
      System.Data.SqlClient.SqlCommand comm=newSystem.Data.SqlClient.SqlCommand();
      comm.CommandText = @"backup database Database1 to disk ='c:\Users\EHSAN\Documents\Visual Studio 2010\Projects\hokm\hokm\DBbackup.bak' with init,stats=10";
      comm.Connection = con;
      comm.ExecuteNonQuery();
      con.Close();

      and when I run it, I get this error: Database 'Database1' does not exist. Make sure that the name is entered correctly. BACKUP DATABASE is terminating abnormally. but I use the database1 and I connect it successfully! what is the problem?

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Uhm you are naming your backup file with the .aspx extension, pretty sure that is going to bite you later, normal extension is .bak

      Never underestimate the power of human stupidity RAH

      S 1 Reply Last reply
      0
      • M Mycroft Holmes

        Uhm you are naming your backup file with the .aspx extension, pretty sure that is going to bite you later, normal extension is .bak

        Never underestimate the power of human stupidity RAH

        S Offline
        S Offline
        sara setare
        wrote on last edited by
        #3

        ohhh,it was a typing mistake , I still have this error ! and another note is that the DBBackup.bak file doesn't exsit for the first time and I wanted to use the directory.createdirectory,file.exist or directory.exist method but I dont know what name space(s) should I use! any suggession for my tow questions???? plz help me!

        1 Reply Last reply
        0
        • S sara setare

          Hi all,I have a problem with back up command! I use this code but it doesnt work:

          string constr = @"DataSource=.\SQLEXPRESS;AttachDbFilename=C:\Users\EHSAN\Documents\Visual Studio 2010\Projects\hokm\hokm\App_Data\Database1.mdf;Integrated Security=True;User Instance=True;";
          System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection ();
          con.ConnectionString = constr;
          con.Open();
          System.Data.SqlClient.SqlCommand comm=newSystem.Data.SqlClient.SqlCommand();
          comm.CommandText = @"backup database Database1 to disk ='c:\Users\EHSAN\Documents\Visual Studio 2010\Projects\hokm\hokm\DBbackup.bak' with init,stats=10";
          comm.Connection = con;
          comm.ExecuteNonQuery();
          con.Close();

          and when I run it, I get this error: Database 'Database1' does not exist. Make sure that the name is entered correctly. BACKUP DATABASE is terminating abnormally. but I use the database1 and I connect it successfully! what is the problem?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          sara-setare wrote:

          but I use the database1 and I connect it successfully!

          Are the connectionstrings the same? Sql Server databases are usually attached to that server, with the server assigning a database-alias to that attached file. You are attaching a file "on the fly", without specifying an alias. I doubt that Sql Server will use the filename as the database-alias. Add a "Database=database1;" part to your connectionstring, similar to the example given below;

          Server=.\SQLExpress;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;Database=dbname;
          Trusted_Connection=Yes;

          I'd recommend attaching it permanently to the server to avoid collisions in alias-names when attaching.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          S 1 Reply Last reply
          0
          • L Lost User

            sara-setare wrote:

            but I use the database1 and I connect it successfully!

            Are the connectionstrings the same? Sql Server databases are usually attached to that server, with the server assigning a database-alias to that attached file. You are attaching a file "on the fly", without specifying an alias. I doubt that Sql Server will use the filename as the database-alias. Add a "Database=database1;" part to your connectionstring, similar to the example given below;

            Server=.\SQLExpress;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;Database=dbname;
            Trusted_Connection=Yes;

            I'd recommend attaching it permanently to the server to avoid collisions in alias-names when attaching.

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

            S Offline
            S Offline
            sara setare
            wrote on last edited by
            #5

            Thank you so much,it works! but maybe a primary question!!: how can I attach my database permanently to the server?

            L 1 Reply Last reply
            0
            • S sara setare

              Thank you so much,it works! but maybe a primary question!!: how can I attach my database permanently to the server?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              You're welcome. Easiest is using the Sql Management Studio, but it could also be done using TSQL;

              sp_attach_db [ @dbname= ] 'dbname'
              , [ @filename1= ] 'filename_n' [ ,...16 ]

              Source[^]

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups