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. can not copy database files

can not copy database files

Scheduled Pinned Locked Moved C#
databasesharepointsecurityhelp
3 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.
  • M Offline
    M Offline
    msrezapro
    wrote on last edited by
    #1

    hi.i want copy database files.when i do detach database throw exeption.i search web and find things.but i cant drive result. please help me.thanks.

    string ConnStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\mydb.mdf"
    +";Integrated Security=True;User Instance=True";
    SqlConnection mycon = new SqlConnection(ConnStr);
    SqlCommand mycom = new SqlCommand("sp_detach_db @d=db_name()", mycon);
    mycon.Open();
    mycom.ExecuteScalar();
    mycon.Close();

    X D 2 Replies Last reply
    0
    • M msrezapro

      hi.i want copy database files.when i do detach database throw exeption.i search web and find things.but i cant drive result. please help me.thanks.

      string ConnStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\mydb.mdf"
      +";Integrated Security=True;User Instance=True";
      SqlConnection mycon = new SqlConnection(ConnStr);
      SqlCommand mycom = new SqlCommand("sp_detach_db @d=db_name()", mycon);
      mycon.Open();
      mycom.ExecuteScalar();
      mycon.Close();

      X Offline
      X Offline
      xstoneheartx
      wrote on last edited by
      #2

      Try these:

      string ConnStr = @"Data Source=.\SQLEXPRESS;Database=Master;Integrated Security=True";
      SqlConnection mycon = new SqlConnection(ConnStr);
      SqlCommand mycom = new SqlCommand("sp_detach_db", mycon);
      mycom.CommandType= CommandType .StoredProcedure ;
      mycom.Parameters.Add(new SqlParameter("@dbname", SqlDbType.NVarChar,50));
      mycom.Parameters["@dbname"].Value = "mydb";
      mycon.Open();
      mycom.ExecuteScalar();
      mycon.Close();

      1 Reply Last reply
      0
      • M msrezapro

        hi.i want copy database files.when i do detach database throw exeption.i search web and find things.but i cant drive result. please help me.thanks.

        string ConnStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\mydb.mdf"
        +";Integrated Security=True;User Instance=True";
        SqlConnection mycon = new SqlConnection(ConnStr);
        SqlCommand mycom = new SqlCommand("sp_detach_db @d=db_name()", mycon);
        mycon.Open();
        mycom.ExecuteScalar();
        mycon.Close();

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        What was not pointed out is that you cannot detach a database the your using. You have to execute the detach sp connected to the master db in SQL Server.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008
        But no longer in 2009...

        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