I created a new item > local database. I'd like to access the data from the database but I can't open a connection. I have,

SqlConnection con = new SqlConnection("DataSource=|DataDirectory|\\testDB.sdf");

debugging will always halt at

con.Open();

Did you try removing the |dataDirectory|\\ portion? What error did you get?

I get the error
"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"

Since I was using sqlserverCE as the database type (or something like that) I needed to include the namespace

using System.Data.SqlServerCe

but I had to add a reference to get it to be included.

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.