Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I got stuck in some code. I'm working now on the return of the SQL database (note that - no SQL SERVER MANAGEMENT STUDIO) built into the MDF file VISUAL STUDIO The code looks like this:
C#
using (SqlCommand cmd = new SqlCommand(query, MyConnection))
{
cmd.Notification = null;
DataTable dt = new DataTable();

SqlDependency dependency = new SqlDependency(cmd);
if (MyConnection.State == ConnectionState.Closed)
MyConnection.Open();


var rdr = cmd.ExecuteReader();
dt.Load(rdr);
if (dt.Rows.Count > 0)
{
name = dt.Rows[0]["ProdName"].ToString();
price = int.Parse(dt.Rows[0]["Price"].ToString());
image = dt.Rows[0]["Images"].ToString();

}

dependency.OnChange += new OnChangeEventHandler

(dependency_OnChange);

..........
But for some reason it does not work and appears to me an error that says:
"A database with the same name exists, or specified file cannot be opened, or it is located on UNC share"

I also updated the file WEB.CONFIG

caching
sqlCacheDependency pollTime="500" enabled="true"

add name="Dataprod" connectionStringName="ConnectionString"
databases
sqlCacheDependency
caching


........

And it does not work

Thanks
Posted
Updated 15-May-15 3:16am
v3

1 solution

The error, "A database with the same name exists ...", suggests that there's a problem with your connection string.

Most likely, you've specified AttachDbFileName, but forgotten to specify User Instance=true;. As a result, SQL has attached your MDF file as a regular database; when it tries to attach it a second time, the database already exists, and you get this error.

To solve the problem, manually detach the existing database, and add the missing setting to your connection string.
 
Share this answer
 
Comments
Member 11516170 15-May-15 10:21am    
So what should I do
name="ConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Dataprod.mdf;Integrated Security=True;" providerName="System.Data.SqlClient"


Thank you in advance for your help
Richard Deeming 15-May-15 10:22am    
Start by reading my answer!
Member 11516170 15-May-15 10:26am    
Sorry, I'm not the best that toward English
Right now I'm working on it
Thanks

תודה

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900