views:

106

answers:

1

Hi,

I'm working on a winforms desktop application that needs to store data. I made the really bad decision to try and embed a database. I've tried:

  • SQLite
  • VistaDB
  • SQL Server Compact

In each case, I was able to generate a Entity Framework Model over the basic schema I've created. I have an event that adds data that I've been using to test these databases.

Well, I kept adding a new record using EF and finding it didn't actually insert a record. In debugging, I checked the context object to see what was happening. It turns out that it is saying "the underlying provider failed to open," or something to that effect. It was not throwing an exception, just not inserting a record.

The same thing has happened for all 3 embedded databases--prompting me to get it through my dense head that there has to be something wrong with my configuration.

Well, I tried to write some basic sql using a sqlconnection and sqlcommand. This time it throws an exception. In the SQL Server Compact case, it now says:

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)

I thought perhaps a problem was the path in app.Config. So I changed the connection string to:

Note that I simplified the path away from anything that might have spaces and avoided using the Data Directory nonsense that causes problem when the debugging directory does not match the preconfigured value for the data directory. I'm running Windows 7; I thought perhaps it might be an access issue--so I tried running VS 2010 in Administrator mode. No luck. I also installed Sql Server Compact SP2, thinking this might be a bug. No luck.

Anyway, I'm ready to pull my hair out. I'm on a tight deadline for this thing and didn't expect to spend the day trying to figure out what is going on.

+1  A: 

Use SqlCeConnection

volody
Okay, this fixed it--at least when it comes to using SqlConnection and SQLCommands.What about the Entity Framework? Is it possible to use SQL Server Compact with the EF?
rsteckly
Hmm, I have not used it. You can check about limitations on SQL Server Compact 3.5 Service Pack 1 Books Online, Entity Framework (SQL Server Compact) http://technet.microsoft.com/en-us/library/cc835494%28SQL.100%29.aspx
volody
So it DOES support EF. However, there is one limitation that I noticed immediately. SQL Server Compact 3.5 SP1 does not support autoincrementing primary keys when using the EF. You need to increment the keys yourself. I used an extension method I found to get the most recent id and increment the number up. I still had to manually assign the value each time, however.
rsteckly