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");

Recommended Answers

All 3 Replies

I suggest trying writing the source full name as follow:

source=computer name\SQLEXPRESS

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

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;
commented: Thanks very much +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.