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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. To attach database from c# coding

To attach database from c# coding

Scheduled Pinned Locked Moved C#
databasehelpcsharpsysadmin
12 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.
  • N NarendraSinghJTV

    I m having a windows application which generates database through coding. I want to attach that generated database to sql-server through coding and than i want to take a backup of that database through a stored procedure. Can anybody help me with this problem.

    Regards Narendra Singh (Jindal Tech Ventures)

    N Offline
    N Offline
    Not Active
    wrote on last edited by
    #2

    NarendraSinghJTV wrote:

    application which generates database through coding

    How are you doing this? It would be the same way on a SQL Server.


    I know the language. I've read a book. - _Madmatt

    N 1 Reply Last reply
    0
    • N Not Active

      NarendraSinghJTV wrote:

      application which generates database through coding

      How are you doing this? It would be the same way on a SQL Server.


      I know the language. I've read a book. - _Madmatt

      N Offline
      N Offline
      NarendraSinghJTV
      wrote on last edited by
      #3

      I m using stored procedure for all creating new database, backup of that generated database. sometimes it shows the error like 'cannot open the backup device'.

      Regards Narendra Singh (Jindal Tech Ventures)

      1 Reply Last reply
      0
      • N NarendraSinghJTV

        I m having a windows application which generates database through coding. I want to attach that generated database to sql-server through coding and than i want to take a backup of that database through a stored procedure. Can anybody help me with this problem.

        Regards Narendra Singh (Jindal Tech Ventures)

        H Offline
        H Offline
        Henry Minute
        wrote on last edited by
        #4

        There is a built in stored procedure sp_attach_db which will accomplish the first part for you. Refer to BooksOnLine documentation or MSDN for more details. To accomplish the backup and for an example using sp_attach_db download the SQLServer 2008 samples from MSDN, if you haven't already, extract the files and then do a text search in the Samples\Engine\Programmability\SMO folder for the above stored proc and for 'backup' There is also a very good article on backups on MSSQLTips.com, just use the search on the home page for 'Backup and Restore SQL Server databases programmatically with SMO'. Good luck! :)

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

        N 1 Reply Last reply
        0
        • H Henry Minute

          There is a built in stored procedure sp_attach_db which will accomplish the first part for you. Refer to BooksOnLine documentation or MSDN for more details. To accomplish the backup and for an example using sp_attach_db download the SQLServer 2008 samples from MSDN, if you haven't already, extract the files and then do a text search in the Samples\Engine\Programmability\SMO folder for the above stored proc and for 'backup' There is also a very good article on backups on MSSQLTips.com, just use the search on the home page for 'Backup and Restore SQL Server databases programmatically with SMO'. Good luck! :)

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

          N Offline
          N Offline
          NarendraSinghJTV
          wrote on last edited by
          #5

          but i think sp_attach_db works only if that database is previously dettached from the server

          Regards Narendra Singh (Jindal Tech Ventures)

          H 1 Reply Last reply
          0
          • N NarendraSinghJTV

            but i think sp_attach_db works only if that database is previously dettached from the server

            Regards Narendra Singh (Jindal Tech Ventures)

            H Offline
            H Offline
            Henry Minute
            wrote on last edited by
            #6

            I have used it for new Databases. Worked fine!

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            N 1 Reply Last reply
            0
            • H Henry Minute

              I have used it for new Databases. Worked fine!

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

              N Offline
              N Offline
              NarendraSinghJTV
              wrote on last edited by
              #7

              can backup work fine after attach database to server, i have attached database to server but it is not finding the database file name.

              Regards Narendra Singh (Jindal Tech Ventures)

              H 1 Reply Last reply
              0
              • N NarendraSinghJTV

                can backup work fine after attach database to server, i have attached database to server but it is not finding the database file name.

                Regards Narendra Singh (Jindal Tech Ventures)

                H Offline
                H Offline
                Henry Minute
                wrote on last edited by
                #8

                I am at the moment searching my own code for suitable example. Will get back to you in a while.

                Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                N 1 Reply Last reply
                0
                • H Henry Minute

                  I am at the moment searching my own code for suitable example. Will get back to you in a while.

                  Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                  N Offline
                  N Offline
                  NarendraSinghJTV
                  wrote on last edited by
                  #9

                  ok i m waiting for your positive reply, one more thing can we call sp_attach_db stored procedure programatically

                  Regards Narendra Singh (Jindal Tech Ventures)

                  H 1 Reply Last reply
                  0
                  • N NarendraSinghJTV

                    ok i m waiting for your positive reply, one more thing can we call sp_attach_db stored procedure programatically

                    Regards Narendra Singh (Jindal Tech Ventures)

                    H Offline
                    H Offline
                    Henry Minute
                    wrote on last edited by
                    #10

                    Here is a snippet which doesn't use sp_attach_db, since it seems to be giving you some problems.

                        public static void AttachDatabase(Server server)
                        {
                            string databaseName;
                            StringCollection dbFiles = new StringCollection();
                            Dialogs.DBFilesDialog dbfd = new Dialogs.DBFilesDialog();
                            if (dbfd.ShowDialog() == DialogResult.OK)
                            {
                                dbFiles.Add(dbfd.PrimaryFileName);
                                dbFiles.AddRange(dbfd.DataFiles);
                                dbFiles.AddRange(dbfd.LogFiles);
                                databaseName = dbfd.DatabaseName;
                    
                                // Attach the database
                                if ((dbFiles\[0\] != null) && (dbFiles\[0\] != string.Empty))
                                {
                                    // Are you sure?  Default to No.
                                    if (MessageBox.Show(@"Do you really want to Attach the " + dbFiles\[0\] +
                                        @" database?", Application.ProductName, MessageBoxButtons.YesNo,
                                        MessageBoxIcon.Question,
                                        MessageBoxDefaultButton.Button2) == DialogResult.No)
                                    {
                                        return;
                                    }
                    
                                    server.AttachDatabase(databaseName, dbFiles, AttachOptions.RebuildLog);
                                }
                            }
                        }
                    

                    The server object is an instance of the Server class from the Microsoft.SqlServer.Management.Smo namespace. Dialogs.DBFilesDialog is a self built Dialog which simply enables the user to select the primary files and log files for the database in one place. Basically it is two ListBoxes, each with an associated button which, when clicked, presents an OpenFileDialog filtered for *.mdf and *.ldf respectively. It has properties for the Primary File name (string), DataFiles (string[]) and LogFiles (string[]). The code is from a VS2005 SQLUtilities library of mine but should translate directly to VS2008(10?) with no problems.

                    Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                    N 1 Reply Last reply
                    0
                    • H Henry Minute

                      Here is a snippet which doesn't use sp_attach_db, since it seems to be giving you some problems.

                          public static void AttachDatabase(Server server)
                          {
                              string databaseName;
                              StringCollection dbFiles = new StringCollection();
                              Dialogs.DBFilesDialog dbfd = new Dialogs.DBFilesDialog();
                              if (dbfd.ShowDialog() == DialogResult.OK)
                              {
                                  dbFiles.Add(dbfd.PrimaryFileName);
                                  dbFiles.AddRange(dbfd.DataFiles);
                                  dbFiles.AddRange(dbfd.LogFiles);
                                  databaseName = dbfd.DatabaseName;
                      
                                  // Attach the database
                                  if ((dbFiles\[0\] != null) && (dbFiles\[0\] != string.Empty))
                                  {
                                      // Are you sure?  Default to No.
                                      if (MessageBox.Show(@"Do you really want to Attach the " + dbFiles\[0\] +
                                          @" database?", Application.ProductName, MessageBoxButtons.YesNo,
                                          MessageBoxIcon.Question,
                                          MessageBoxDefaultButton.Button2) == DialogResult.No)
                                      {
                                          return;
                                      }
                      
                                      server.AttachDatabase(databaseName, dbFiles, AttachOptions.RebuildLog);
                                  }
                              }
                          }
                      

                      The server object is an instance of the Server class from the Microsoft.SqlServer.Management.Smo namespace. Dialogs.DBFilesDialog is a self built Dialog which simply enables the user to select the primary files and log files for the database in one place. Basically it is two ListBoxes, each with an associated button which, when clicked, presents an OpenFileDialog filtered for *.mdf and *.ldf respectively. It has properties for the Primary File name (string), DataFiles (string[]) and LogFiles (string[]). The code is from a VS2005 SQLUtilities library of mine but should translate directly to VS2008(10?) with no problems.

                      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                      N Offline
                      N Offline
                      NarendraSinghJTV
                      wrote on last edited by
                      #11

                      thanks henry for your precious code but i m using object database not smo

                      Regards Narendra Singh (Jindal Tech Ventures)

                      H 1 Reply Last reply
                      0
                      • N NarendraSinghJTV

                        thanks henry for your precious code but i m using object database not smo

                        Regards Narendra Singh (Jindal Tech Ventures)

                        H Offline
                        H Offline
                        Henry Minute
                        wrote on last edited by
                        #12

                        It's a pleasure. :) Good luck.

                        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                        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