SqlException was unhandled

R

Roy Gourgi

Hi,

I am getting this error message:

SqlException was unhandled - An attempt to attach an auto-named database for
file D:\CombinatoRoyX\DB\MyDatabase#1.mdf failed. A database with the same
name exists, or specified file cannot be opened, or it located on UNC share.

I am getting the error message on this line - oCommand.Connection.Open();


Here is my code:


CODE
public class TestDB
{

public static void mTestDB() // ********** mAA2_EnumMP_Top()
{

int lnNoParameters = 3;

int[] laSignature = new int[lnNoParameters];
int[] laReader = new int[lnNoParameters];

string strConnection = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=D:\CombinatoRoyX\DB\MyDatabase#1.mdf;Integrated Security=True;Connect Timeout=5;User Instance=True";
string strCommMasterInsert = "INSERT INTO MyDatabase#1 (P0, P1, P2)
SELECT @par0, @par1, @par2";

string strCommDelete = "DELETE FROM MyDatabase#1";

SqlConnection oConnection = new SqlConnection(strConnection);
SqlCommand oCommand = new SqlCommand(strCommMasterInsert,
oConnection);


oCommand.CommandText = strCommDelete;
oCommand.Connection.Open();
oCommand.ExecuteNonQuery();
oCommand.Connection.Close();

oCommand.CommandText = strCommMasterInsert;
oCommand.Connection.Open();
oCommand.ExecuteNonQuery();
oCommand.Connection.Close();


}

}
 
A

Aaron

The problem is with your connection string. In it you are telling SQL server
that you want to attach a specific database to it and use that database for
the connection. The database is obviously already attached, so your
connection string should reflect that.

strString = "data source=SQLServerName;initial catalog=YourDBName;user
id=DBUserName;password=DBPassword;persist security info=True"

This particular connection string might not work for you.

Best way to get the connection string is to connect through the Server
Explorer in Visual Studio and look at the properties of the connection.

Here is another good source :
http://www.connectionstrings.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top