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. General Programming
  3. C#
  4. Attachment Database

Attachment Database

Scheduled Pinned Locked Moved C#
databasesql-serversysadmin
3 Posts 2 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.
  • T Offline
    T Offline
    The_Soul_Of_Rock
    wrote on last edited by
    #1

    In MS SQL Server, it's got an attacth and dettach database, so i want to write a programme to do that, but i do not know where to start. Thanks Rock Throught The Night

    H 1 Reply Last reply
    0
    • T The_Soul_Of_Rock

      In MS SQL Server, it's got an attacth and dettach database, so i want to write a programme to do that, but i do not know where to start. Thanks Rock Throught The Night

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Use a SqlCommand to execute them, just like you'd type them in a command parser (like the Query Analyzer or osql.exe). Use a SqlConnection that specifies the 'master' database as the 'Initial Catalog', however. The 'master' database must always exist, otherwise you can't do anything, so it's a good database to attach to when running commands like that. A simple example (without any error checking, like seeing if the files actually exist first):

      public void AttachDatabase(string name, string mdfFile, string ldfFile)
      {
      using (SqlConnection conn = new SqlConnection(
      "Data Source=.;Initial Catalog=master;Integrated Security=SSPI"))
      {
      SqlCommand cmd = conn.CreateCommand();
      cmd.CommandText = "exec sp_attach_db @dbname, @filename1, @filename2";
      cmd.Parameters.Add("@dbname", SqlDbType.NVarChar, 128).Value = name;
      cmd.Parameters.Add("@filename1", SqlDbType.NVarChar, 260).Value = mdfFile;
      cmd.Parameters.Add("@filename2", SqlDbType.NVarChar, 260).Value = ldfFile;
       
      conn.Open();
      cmd.ExecuteNonQuery();
      conn.Close();
      }
      }

      See the documentation for the sp_attach_db and sp_detach_db stored procs, as well as the SqlCommand.Parameters collection property for more information.

      Microsoft MVP, Visual C# My Articles

      T 1 Reply Last reply
      0
      • H Heath Stewart

        Use a SqlCommand to execute them, just like you'd type them in a command parser (like the Query Analyzer or osql.exe). Use a SqlConnection that specifies the 'master' database as the 'Initial Catalog', however. The 'master' database must always exist, otherwise you can't do anything, so it's a good database to attach to when running commands like that. A simple example (without any error checking, like seeing if the files actually exist first):

        public void AttachDatabase(string name, string mdfFile, string ldfFile)
        {
        using (SqlConnection conn = new SqlConnection(
        "Data Source=.;Initial Catalog=master;Integrated Security=SSPI"))
        {
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = "exec sp_attach_db @dbname, @filename1, @filename2";
        cmd.Parameters.Add("@dbname", SqlDbType.NVarChar, 128).Value = name;
        cmd.Parameters.Add("@filename1", SqlDbType.NVarChar, 260).Value = mdfFile;
        cmd.Parameters.Add("@filename2", SqlDbType.NVarChar, 260).Value = ldfFile;
         
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        }
        }

        See the documentation for the sp_attach_db and sp_detach_db stored procs, as well as the SqlCommand.Parameters collection property for more information.

        Microsoft MVP, Visual C# My Articles

        T Offline
        T Offline
        The_Soul_Of_Rock
        wrote on last edited by
        #3

        thank you very much.;) Rock Throught The Night

        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