Search code examples
c#wcfsql-server-2008wcf-data-serviceswcf-binding

Attaching a SQL server express database with WCF service application


I am writing a WCF service application and my front end that is consuming is winforms.

I am trying to connect to SQL Express in WCF as database, the issue i face is the connection string, i have a database created under app folder in WCF project,

This is my connection string in wcf project:

SqlConnection myConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;
AttachDbFilename=\\App_Data\\Database1.mdf;
Integrated Security=True;User Instance=True");

I get this error,

SqlException was unhandled by user code

An attempt to attach an auto-named database for file \App_Data\Database1.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

How do i attach a SQL Server Express database in a WCF service application project (with appropriate connection string) so that the consuming application can view the data from this DB? Any help will be highly helpful? Thanks.


Solution

  • Use the following code for the SqlConnection

         SqlConnection conn = new SqlConnection(@"data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\Database1.mdf;User Instance=true");