Home » .Net Framework

how to attach sql database file to creating .exe setup in c#

I create a Setup file and put data folder, in data folder i put database file , but the problem is i have to mannualy attach database file in sql server 2005. I have to automatically attach database file when installing setup file.

1 Answer Found


Answer 1

Hi bhagvad,

Welcome to MSDN Forums!


We can implement auto attach  database just use the connection string following.


1)       Add a folder  named ”DB” to the project, and copy the database  file into it after you detach it from your sql  server.

2)       Change your connection string like this:

        <add name="WindowsFormsApplication1.Properties.Settings.BabakConnectionString"
            connectionString="Data Source=.;AttachDbFilename=|DataDirectory|\DB\Babak.mdf;Initial Catalog=test;Integrated Security=True"
            providerName="System.Data.SqlClient" />


3)       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.


If there’s anything unclear, please feel free to let me know.


Best wishes,



Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.



<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure