Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection string with an SQL Server CE database

I'm trying to establish a connection with a database in a Windows CE 5.0 application and I'm using the Compact Framework 2.0

The database is located inside the project's folder:

C:\Documents and Settings\softdil\My Documents\Visual Studio 2008\Projects\Datalogic\Datalogic

These are the lines I'm using in order to connect and open the database:

SqlCeConnection conn = new SqlCeConnection();
conn.ConnectionString = "Data Source = Datalogic.sdf;";
conn.Open();

Which gives me a beautiful "database file not found" error message.

I also tried with the absolute uri with same results:

conn.ConnectionString = "Data Source = C:\\Documents and Settings\\softdil\\My Documents\\Visual Studio 2008\\Projects\\Datalogic\\Datalogic;";

What am I doing wrong here?

May be it has something to do with the aplication being debugged (executed) in the mobile device?

I don't really think so because the database works when the application is loaded, meaning that is associated with a listbox and loads data correctly from the database.

like image 775
rfc1484 Avatar asked Jul 15 '26 04:07

rfc1484


2 Answers

In a Win CE application we use the following to get the full path of the executing file:

string StartupPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

Using StartupPath you can then add your database name to that path and add it to the connection string:

string datalogicFilePath = Path.Combine(StartupPath, "Datalogic.sdf");
string connectionString = string.Format("DataSource={0}", datalogicFilePath);
like image 163
Fredrik Ljung Avatar answered Jul 17 '26 18:07

Fredrik Ljung


You will have to make it work for WinCE, I don't think you have a C:\\Documents and Settings\\ on your target.

I don't really think so because the database works when ...

Look in your App.Config, maybe you already have a connectionstring?

like image 44
Henk Holterman Avatar answered Jul 17 '26 18:07

Henk Holterman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!