How do i attach a sql express .mdf file which is located in a certain folder on my C: drive to sql express in my code, there are many examples on the web but none of them seem to give a code example which works, I am using visual Studio 2008 and Sql Express 2005
Attach sql mdf file in code
Collapse
X
-
I think this should work:
Code:dim cnnConnection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
Steven -
Database Attach
Thanks, that works, but it does not persist the attachment in sql manager,
Code:Dim cnnConnection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=" & Application.StartupPath + "\datafiles\" + TextBox1.Text + ".mdf" & ";Integrated Security=True;Connect Timeout=30;User Instance=True")
Last edited by Frinavale; Apr 14 '09, 04:10 PM. Reason: Added code tags. Please post code snippets in [code] [/code] tags.Comment
-
I got it to work, thanks for all the excellent help, this is the solution
Code:Dim cnnConnection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=" & Application.StartupPath + "\datafiles\" + TextBox1.Text + ".mdf" & ";Integrated Security=True;Database='" & TextBox1.Text & "';Connect Timeout=30") cnnConnection.Open() cnnConnection.Close()
Last edited by Frinavale; Apr 14 '09, 04:10 PM. Reason: Added code tags. Please post code snippets in [code] [/code] tags.Comment
Comment