0

In the output directory of my solution there's a folder called Storage. In this folder is a database file Comments.sdf with no password.

Now I want to connect to this database by the following code:

string connectionString = @"Data Source=\Storage\Comments.sdf;Persist Security Info=False;";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();

The line connection.Open(); throws an exception.

I only have the german error-message:

Netzwerkbezogener oder instanzspezifischer Fehler beim Herstellen einer Verbindung mit SQL Server. Der Server wurde nicht gefunden, oder auf ihn kann nicht zugegriffen werden. Überprüfen Sie, ob der Instanzname richtig ist und ob SQL Server Remoteverbindungen zulässt. (provider: SQL Network Interfaces, error: 25 - Verbindungszeichenfolge ungültig)

In english it's like:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or it can not be accessed. Verify that the instance name is correct and that SQL Server allows remote connections. (provider: SQL Network Interfaces, error: 25 - Connection string is invalid)

Is there anything wrong with my connection string or do I have to set some properties at the database file?

4
  • You do realize you need a sql-server running to actually connect to that file, right? Commented Aug 18, 2013 at 22:06
  • If you are using sql server compact, which I believe is the case here, no running sql server instance is required. Commented Aug 18, 2013 at 22:09
  • Could it be that the problem is that I've create the databasefile with sql-server 2008 r2 and implement the solution in VS2012? Commented Aug 18, 2013 at 22:10
  • @Tarec: NO he does not! It's a SQL Server Compact Edition file - this is a stand-alone database file, just a few DLLs needed, NO SERVER installation needed! Commented Aug 19, 2013 at 4:45

1 Answer 1

2

Your connection string is specifing an invalid path to the database file. Simply remove the first /

string connectionString = @"Data Source=Storage\Comments.sdf;Persist Security Info=False;";
SqlCeConnectionconnection = new SqlCeConnection(connectionString);
connection.Open();

be sure to reference System.Data.SqlServerCe

Sign up to request clarification or add additional context in comments.

6 Comments

No, this doesn't solve the problem. I also tried a absolute path, and it still doesn't work
I received the same message
I have updated my post, add a reference to System.Data.SqlServerCe and change your connection to SqlCeConnection
Ok this solved my problem. But now I get another exception: A first chance exception of type 'System.Data.SqlServerCe.SqlCeInvalidDatabaseFormatException' occurred in System.Data.SqlServerCe.dll
Sounds like you have a version mismatch between client and file format
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.