1

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.

Sharpeye500
  • 8,775
  • 25
  • 95
  • 143

2 Answers2

2

Use the following code for the SqlConnection

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

Check this site:

  1. connectionstrings.com

  2. Connecting to sql server database mdf file without installing sql server on client machine

and try this connectionstring also:

SqlConnection myConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;
Initial Catalog=Database1;
Integrated Security=True;User Instance=True");
Community
  • 1
  • 1
BizApps
  • 6,048
  • 9
  • 40
  • 62