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. C# sp_attach_db execute

C# sp_attach_db execute

Scheduled Pinned Locked Moved C#
csharpdatabasehelptutorialquestion
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.
  • B Offline
    B Offline
    betterc
    wrote on last edited by
    #1

    I need to detach and then re-attach my MSDE db from within my C# application. Can someone give me a clue on how to do this? I have no problem doing it in Querey Analyzer but can't seem to make the transition into C3? Thanks, cb

    H 1 Reply Last reply
    0
    • B betterc

      I need to detach and then re-attach my MSDE db from within my C# application. Can someone give me a clue on how to do this? I have no problem doing it in Querey Analyzer but can't seem to make the transition into C3? Thanks, cb

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

      Connect to the master database and simply execute the sp_attach_db stored proc on the database (using all three parameters, using paths local to the target machine - not yours!) like so:

      SqlConnection conn = new SqlConnect(
      "Integrated Security=SSPI;Data Source=DBSRV1;Initial Catalog=master");
      SqlCommand cmd = conn.CreateCommand();
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.CommandText = "sp_attach_db";
      cmd.Parameters.Add("@dbname", SqlDbType.NVarChar, 128).Value = "MyDB";
      cmd.Parameters.Add("@filename1", SqlDbType.NVarChar, 260).Value =
      @"C:\Program Files\Microsoft SQL Server\MSSQL\Data\MyDB.mdf";
      cmd.Parameters.Add("@filename2", SqlDbType.NVarChar, 260).VAlue =
      @"C:\Program Files\Microsoft SQL Server\MSSQL\Data\MyDB.ldf";
      try
      {
      conn.Open();
      cmd.ExecuteNonQuery();
      }
      finally
      {
      conn.Close();
      }

      Microsoft MVP, Visual C# My Articles

      B 1 Reply Last reply
      0
      • H Heath Stewart

        Connect to the master database and simply execute the sp_attach_db stored proc on the database (using all three parameters, using paths local to the target machine - not yours!) like so:

        SqlConnection conn = new SqlConnect(
        "Integrated Security=SSPI;Data Source=DBSRV1;Initial Catalog=master");
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "sp_attach_db";
        cmd.Parameters.Add("@dbname", SqlDbType.NVarChar, 128).Value = "MyDB";
        cmd.Parameters.Add("@filename1", SqlDbType.NVarChar, 260).Value =
        @"C:\Program Files\Microsoft SQL Server\MSSQL\Data\MyDB.mdf";
        cmd.Parameters.Add("@filename2", SqlDbType.NVarChar, 260).VAlue =
        @"C:\Program Files\Microsoft SQL Server\MSSQL\Data\MyDB.ldf";
        try
        {
        conn.Open();
        cmd.ExecuteNonQuery();
        }
        finally
        {
        conn.Close();
        }

        Microsoft MVP, Visual C# My Articles

        B Offline
        B Offline
        betterc
        wrote on last edited by
        #3

        Thanks Heath, I'm getting an "Unrecognized escape sequence" for the values for filename1 and filename2. Doesn't like the '\'. I tried putting the value in paren's but that didn't help either. cb

        M B H 3 Replies Last reply
        0
        • B betterc

          Thanks Heath, I'm getting an "Unrecognized escape sequence" for the values for filename1 and filename2. Doesn't like the '\'. I tried putting the value in paren's but that didn't help either. cb

          M Offline
          M Offline
          Michael P Butler
          wrote on last edited by
          #4

          betterc wrote: I'm getting an "Unrecognized escape sequence" for the values for filename1 and filename2. Doesn't like the '\'. You'll need to use \\ rather than a single \ Michael CP Blog [^]

          1 Reply Last reply
          0
          • B betterc

            Thanks Heath, I'm getting an "Unrecognized escape sequence" for the values for filename1 and filename2. Doesn't like the '\'. I tried putting the value in paren's but that didn't help either. cb

            B Offline
            B Offline
            betterc
            wrote on last edited by
            #5

            Thanks I figured it out (@"\bla\bla\bla");

            1 Reply Last reply
            0
            • B betterc

              Thanks Heath, I'm getting an "Unrecognized escape sequence" for the values for filename1 and filename2. Doesn't like the '\'. I tried putting the value in paren's but that didn't help either. cb

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

              Either escape them using "\" (so "\\") or use the literal string identifier "@" before the string, like "@C:\Program Files...". Looking at the documentation for the compiler error would've told you that.

              Microsoft MVP, Visual C# My Articles

              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