How to attach/restore sql database file(.mdf) automatically while running setup of a project made in

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have gone reading several threads in this connection but I am unsuccessfull doing it. Kindly help me.
I have made a simple application in VB 2010. I have created a database in SQL 2008 Express. I have added the datbase in VB by using "Add data source" and build my project. My application runs fine in the same machine. Now when I try to use my exe in different
machines , it doesnot run until I restore the database in that machine. I cannot attach or restore the database while running setup in that machine. I have followed some topics related, an example is given below, but could not build it. I got the general error
" LOgin failed ........." . I have used both windows and sql authentication but failed.

<p style="margin:0in 0in 0pt 0.5in; text-indent:-0.25in <span style="font-family:Segoe UI,sans-serif; font-size:10pt <span>1)<span style="font:7pt/normal Times New Roman
<span style="font-family:Segoe UI,sans-serif; font-size:10pt Add a folder named ”DB” to the project, and copy the database file into it after you detach it from your sql server.

<p style="margin:0in 0in 0pt 0.5in; text-indent:-0.25in <span style="font-family:Segoe UI,sans-serif; font-size:10pt <span>2)<span style="font:7pt/normal Times New Roman
<span style="font-family:Segoe UI,sans-serif; font-size:10pt Change your connection string like this:

<p style="margin:0in 0in 0pt 0.25in <span style="color:blue; font-family:Segoe UI,sans-serif; font-size:10pt <<span style="color:#a31515; font-family:Segoe UI,sans-serif; font-size:10pt connectionStrings
<span style="color:blue; font-family:Segoe UI,sans-serif; font-size:10pt >
<span style="font-family:Segoe UI,sans-serif; font-size:10pt <br/>
<span style="color:blue <<span style="color:#a31515 add
<span style="color:blue <span style="color:red name <span style="color:blue
= "<span style="color:blue WindowsFormsApplication1.Properties.Settings.BabakConnectionString "<br/>
<span style="color:blue
<span style="color:red connectionString <span style="color:blue = "<span style="color:blue Data Source=.;AttachDbFilename=|DataDirectory|DBBabak.mdf;Initial Catalog=test;Integrated Security=True "<br/>
<span style="color:blue
<span style="color:red providerName <span style="color:blue = "<span style="color:blue System.Data.SqlClient "<span style="color:blue /><br/>
<span style="color:blue </ <span style="color:#a31515
connectionStrings <span style="color:blue >

<span style="font-family:Segoe UI,sans-serif; font-size:10pt <span>3)<span style="font:7pt/normal Times New Roman
<span style="font-family:Segoe UI,sans-serif; font-size:10pt After these, the database file will auto attach to the sql server when you run your application, and the database name is “test”, you can open the sql server management studio
to find it.

<span style="font-family:Segoe UI,sans-serif; font-size:10pt <span style="font-family:Segoe UI,sans-serif; font-size:10pt In addition, you can find the connection in the app.config file through the solution explorer in vs2010. And you also
can find this file under your project. With these information, you can find it and modify it.
"
I have used the above one but I failed. Can anyone help me ? I am totally depressed as I am trying it for long time but could not. Kindly help me. <br/>




View the full article
 
Back
Top