Hi, i have a program that connects with an sql database and it works fine with the sql. Now the problem comes when i publish. When i open the published program it gives me an error. An attempt to attach an auto-named database for file ..... A database with the same name exists, or specified file cannot be opened, or it is located on a UNC share.

the code for the sqlconnection:

public static SqlConnection conn = new SqlConnection(@"Data Source = .\SQLEXPRESS; AttachDbFilename= |DataDirectory|\dbRest.MDF; Initial Catalog = ; Integrated Security = SSPI; User Instance = False");
8 Years
Discussion Span
Last Post by rozendaa@xs4all

I suggest trying writing the source full name as follow:

source=computer name\SQLEXPRESS

Edited by tweety1313: n/a


Try without the AttachDbFilename part and use a catalog name instead.
E.g. "Data Source=.\SQLEXPRESS;Initial Catalog=MyData;Integrated Security=SSPI"

Edited by nick.crane: n/a


Assuming you distribute your application to multiple machines where you want to use the default instance of Sql Server, here is a copy of the code I use in a program that has the same requirements (allthough the parameters in the connectionstring are different. Crucial is the stament with csb.DataSource)

this.conn = new System.Data.SqlClient.SqlConnection();
    System.Data.SqlClient.SqlConnectionStringBuilder csb = 
         new System.Data.SqlClient.SqlConnectionStringBuilder();
    csb.AsynchronousProcessing = false;
    csb.IntegratedSecurity = true;
    csb.DataSource = System.Environment.MachineName;
    this.conn.ConnectionString = csb.ConnectionString;
Votes + Comments
Thanks very much
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.