473,383 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Attach sql mdf file in code

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
Mar 27 '09 #1
4 9580
MrMancunian
569 Expert 512MB
I think this should work:

Expand|Select|Wrap|Line Numbers
  1. dim cnnConnection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
Of course you'll need to have SQL Express installed.

Steven
Apr 8 '09 #2
aryanbs
42
Here is a good repository of connection strings

http://www.connectionstrings.com/sql-server-2005
Apr 8 '09 #3
Thanks, that works, but it does not persist the attachment in sql manager,
Expand|Select|Wrap|Line Numbers
  1.             Dim cnnConnection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=" & Application.StartupPath + "\datafiles\" + TextBox1.Text + ".mdf" & ";Integrated Security=True;Connect Timeout=30;User Instance=True")
Is it possible to keep this file attached after the program has exited
Apr 9 '09 #4
I got it to work, thanks for all the excellent help, this is the solution
Expand|Select|Wrap|Line Numbers
  1.                 Dim cnnConnection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=" & Application.StartupPath + "\datafiles\" + TextBox1.Text + ".mdf" & ";Integrated Security=True;Database='" & TextBox1.Text & "';Connect Timeout=30")
  2.                 cnnConnection.Open()
  3.                 cnnConnection.Close()
Apr 9 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: tabonni | last post by:
Hi I try to grab the checked files from HTML page and then send those PDF files as attachments. It can just send email, there are no PDF files attached. Can anybody point out my error? My...
1
by: vijaygparikh | last post by:
HI i am writting a code for open default mail client and it works fine. i want to attach a file with the default mail client but file is not attached with "mailto:" option. If any one has idea...
5
by: erez | last post by:
Hello How can i attach an html file to TableRow in HtmlTable control that run at server, the code should be in server side. thanks
4
by: donall | last post by:
i have a DLL which implements a HTTP handler, written in C#, and have deployed it on IIS 5.0, on XP service pack 2. six months ago, i was able to debug it by attaching CLR to the process...
7
by: Greg P | last post by:
I know this is a long post, please bear with me. I have been working on this all weekend to no avail although I have done a good amount of research (see most pertinent links that I've looked at...
2
by: calebmichaud | last post by:
Hi I am having trouble understanding how to attach a file or files to a record. I want to (in form view0 allow the user to attach one or more than one file to the associated record, and then...
0
by: Lexxzy | last post by:
Hi to all, I've a problem about to attach a recovered database file in the enterprise manager. My problem goes this way, I want to attach a recovered database file. So when I followed this...
7
by: RRsonawane11 | last post by:
Hello, Can anyone know how to write asp code to attach one file and email it to a specified email address. The attach file is the client choose from their Local Harddisk, just like we ...
14
by: sreemathy2000 | last post by:
My requirement is to generate and attach a file and send in an email automatically. i have a existing functionality where i using the code below to write a CSV file in my local system. ...
9
by: dpatel75 | last post by:
I am trying to copy a database from a SQL 2000 SP3 Windows 2000 server to a 2005 SP2 Windows 2003 server. I am trying to use detach and attach method (have tried both within Management Studio and...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.