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. Database & SysAdmin
  3. Database
  4. Detaching SQL Database .mdf file !

Detaching SQL Database .mdf file !

Scheduled Pinned Locked Moved Database
databasetutorialquestion
5 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.
  • J Offline
    J Offline
    jeshra279
    wrote on last edited by
    #1

    I have a SQL database .mdf file.I want to detach this database mdf file and then copy the file into a new directory using VisualBasic code. How to do this?

    M A 2 Replies Last reply
    0
    • J jeshra279

      I have a SQL database .mdf file.I want to detach this database mdf file and then copy the file into a new directory using VisualBasic code. How to do this?

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

      I recommend that you BACKUP the database instead of copying the MDF.

      Never underestimate the power of human stupidity RAH

      J 1 Reply Last reply
      0
      • M Mycroft Holmes

        I recommend that you BACKUP the database instead of copying the MDF.

        Never underestimate the power of human stupidity RAH

        J Offline
        J Offline
        jeshra279
        wrote on last edited by
        #3

        I was trying to backup the database file by copying it from one folder to another, but it is throwing error that file is in use. So I thought of detach it first and then copy. So How to do this?

        M 1 Reply Last reply
        0
        • J jeshra279

          I was trying to backup the database file by copying it from one folder to another, but it is throwing error that file is in use. So I thought of detach it first and then copy. So How to do this?

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

          The file is in use because the (SQL) server is using it - always. There are some excellent examples of how to take a backup correctly[^] right here on CP

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          • J jeshra279

            I have a SQL database .mdf file.I want to detach this database mdf file and then copy the file into a new directory using VisualBasic code. How to do this?

            A Offline
            A Offline
            Abhishek Sur
            wrote on last edited by
            #5

            Use this :

            BACKUP DATABASE [DataBaseName]
            TO DISK = N'D:\DataBases\BackupDB.bak' WITH
            NOFORMAT,
            NOINIT,
            NAME = N'DataBaseBackup-Name',
            SKIP,
            NOREWIND,
            NOUNLOAD,
            STATS = 10

            Create an object of SqlCommand and pass this as CommandText. Run this in master Database (I mean the connection object should point to master database) Use ExecuteNonQuery to take backup to Disk location mentioned. :rose:

            Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


            My Latest Articles-->** Microsoft Bing MAP using Javascript
            CLR objects in SQL Server 2005
            Uncommon C# Keywords
            /xml>

            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