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.



I had installed SQL Server 2008R2 and client tools w/o full text search feature and filestream enabled .  As a result, the AdventureWorks sample databases were not installed successfully.  Later on I enabled full text research and filestream from SQL Server Configuration Manager and the AdventureWorks database were installed successfully. 

However, re-running the setup.exe with full text search enabled for installing the client tools did not show the AdventurWorks2008R 2 in SSMS2008, while AdventurWorksDW2008R2 and AdventureWorksLT2008R2, as well as AdventureWorks, AdventureWorksDW and AdventureWorksLT were shown in SSMS2008.  Opening SSMS2008 from database server neither showed the AdventureWorks2008R2.

Are there any differences on running setup exe from database server or client workstation?  Do I need to re-run the setup.exe from
Database server so that the AdbetureWorks2008R2 can be shown in my workstation SSMS2008? 





I created an C# exe which was targeting to "Any CPU". I ran into an issue while using Microsoft.Jet.Oledb.4.0 driver on win7 64 bit os. I found through some site that changing the target platfrom from "Any CPU" to "x86" will fix the issue.

My dev environment is VS2008, Win7 32 bit OS. I changed the target platfrom from "Any CPU" to "x86" and copied all the exe's and dll's to the test machine having win7 64 bit OS to test the app's functinality. It worked all fine there on test machine.

But the PROBELM AROSE today, when i got the seutp file from my installer team and tested it on the win7 64 bit OS machine and found the message "The Microsoft.Jet.Oledb.4.0 provider is not registered on the local machine." I re-ran the application by copying the exe's and dll's to the test machine, but there was no problem.

Please correct me if i am wrong.. "I am suspecting it to the issue while the setup file is created..."

Our installer team is not aware of the target platfrom change. Is it a problem which we need to fix with our installer?

Do we need to change/configure the installer in a way so that the exe installed by setup file can be run under target platform x86?


Hello everyone,

I have a c# setup that install automatically sql server express 2008 (from the prerequisites setup list) and then run a create database script and a fill data script

My problem is that when I run the scripts, just the "use[master]" successful but the "create database" failed with this error message:"CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file 'C:\Program Files..."

Maybe someone know what is the problem with my c# setup and how can I solve it?



I am having problem when i was trying to attach .mdf file while create a new connection of SQL server, create a new connection of SQL database file and open the database in Server Explorer. I am using VS 2010 Ultimate and SQL Express 2008 R2.

Can anyone please tell me what is the problem I am facing?

Here are the error message.

1. Attach .mdf in SQL database file and open the database in Server Explorer

2. Create a new SQL server and attach .mdf


I was trying to attach a database to SQL Server MGT Studio and got an exception error.  The message stated that the database file I was trying to attach could not be opened because it was version 661.  The SQL Server shows to support version 655 and earlier.  It states that a downgrade path is not supported.

Question:  Is there some download ; fix; I can make to SQL Server 2008 that will allow me to attach the version 661????  I am not technical at all.  Please speak slowly.  :)  Thanks


When I try to attach a database using SQL server 2008 express version, it gives me an error message.  it says

"Msg 18456, Level 14, State 1, Server LINDA\SQLEXPRESS, Line 1
Login failed for user 'LINDA\Linda'"

How can I fix this error?



Dear all,

Last month our Windows 2003 server hardware raid was failed, all live databases available on this raid partition, now we have recover all databases which MDF and LDF files.

 Below is the error snap link. http://cid-51b01a6564f704c6.photos.live.com/self.aspx/Kamran/Error%20Snap.png

We are in the process of attaching the Recovered Database “nice_interactions ” using .MDF and .LDF files; it prompts the Following error [Please view the snapshot below for reference]. Kindly advise further how to restore or attach database using .MDF and .LDF files which is not cleanly shutdown.

Microsoft SQL forwarding to following URL in the Detail of below  listed Error while we are trying to attach the recovered .mdf file.



Need help in this regards.

Kamran Khan



Hi.  I've encountered a strange issue in SQL Server Management Studio (not the express version).  I had to uninstall SQL Server 2008 Developer in order to install SQL Server 2008 R2 Developer.  As a result of doing this, the database from one of my ASP.NET MVC projects is not attached to the database server.  I see the ".mdf" file in my project's AppData folder in Windows Explorer, but I can't navigate to it in SQL Server Management Studio.  I go to "C:\Users\<my name>" and the attach database interface will not let me go any further.  The folder that I need to access is "C:\Users\<my name>\Documents\Visual Studio 2010\NerdDinner\NerdDinner\App_Data".  How can I access this directory from SQL Server Management Studio?  Is there some sort of strange permission issue?  Thanks in advance for any help.

Hello Guys,

I need your help

I tried attach .mdf file to server via vb.net code for deployment my application .

I put .mdf and log in debug folder and I used this connection string

Public con As New SqlConnection("Server =.\SQLEXPRESS ;AttachDbFilename=" & My.Application.Info.DirectoryPath.ToString & "\ADHD_DIAGNOSIS_SYSTEM.mdf" & ";Trusted_Connection=Yes")

but when I debuge the app

I got this exception

Directory lookup for the file "C:\Users\Sara\Desktop\Last Version\proj\Graduation Project\Graduation Project\bin\Debug\ADHD_DIAGNOSIS_SYSTEM.mdf" failed with the operating system error 5(‏‏denied access).
Cannot attach the file 'C:\Users\Sara\Desktop\Last Version\proj\Graduation Project\Graduation Project\bin\Debug

I use windows 7

I'm building a win app using VS 2008 in C# using an SQL Express 2005 DB.  What is the best way to include a SQL DB in a setup project.  Does the SQL need to have a .dbo or .mdf extension?  I'm new to SQL Server Express.

 Hell all,

I am trying to figure out how to create the personal_add.sql file GoDaddy needs in order to install a SQL Server database.

I know I probably knee the sqlexportonly method and All method however I can't figure the proper syntax for the database.  I have SQL Studio Express and will first need to Combine the ASPNETDB into the personal database, since my GoDaddy account only allows one SQL Server Database, then I will need to convert this single databse into SQL Server form and not SQL Server Express.  After this is done, I need to create the databasename_add.sql file in order to install it into GoDaddy Server.

I was wondering how to go about doing this.  an example variable named command line would be appreciated.  Thanks



when i try to attach the adventureworksLT(SQL server 2005) to SQL server 2008 it gave me the error:

Attach database failed for Server 'VALENTINO-PC'.  (Microsoft.SqlServer.Smo)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=10.0.1600.22+((SQL_PreRelease).080709-1414+)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Attach+database+Server&LinkId=20476


An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)


Database 'AdventureWorksLT' cannot be upgraded because it is read-only or has read-only files. Make the database or files writeable, and rerun recovery. (Microsoft SQL Server, Error: 3415)


how can I attach sql server services into my C#  project setup



Can any one know how to create exe file of window application in C# .NET 2010 n i want to create readme file in this exe file.Can any one explain the  step?


When I used visual studio to create setup up files, it always create two files: setup.exe and a setup_###.msi.  How can I create just an setup.exe?



I want to include a .exe in to .msi which will be created using VS 2008 setup and deployment. Bascially, I want to run this exe automatically when I run my setup.

Thanks in Advance.




My Problem is simply :

attaching DB build on SQL 2000 (but been attached on SQL 2005) is not possible

could u plz guyz help me solving this PROPLEM

I have created a Windows Application , i want to deploy it aong with Windows Installer 3.1, .NET Framework 2.0 , SQL Server Server Server tools, database along with the Application file onto a single setup file.

How will i do that ? 


I have one output in my deployment project (with correct version from its assemblyInfo) and I consider why version of the setup.exe (file properties) doesn't change... I still find there my VS2010 version (10.0.30319.1). I want to admit that i changed version in deployment project as well.

Any propositions? I will be greatful for help!


I want to create a setup file for a windows application.

I have used XML file as DataBase in my application. I have kept my XML file in bin\debug folder of my application.

I want to know how to include my XML file while creating setup file. Where I need to kept my XML file to be included while creating setup file. Please help me out.

Thanks in advance:)


<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure