开发者

Connection string with an SQL Server CE database

开发者 https://www.devze.com 2023-03-21 01:22 出处:网络
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

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开发者_StackOverflow社区 the application is loaded, meaning that is associated with a listbox and loads data correctly from the database.


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);


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?


Here is how I fixed it, in case anyone happens to get the same problem.

When you execute an application in visual studio into a Windows CE terminal device, the application is copied there and also the database. So the connection string must use an uri related to the terminal and not relative to a path in your computer.


Give Path of DB File in Data Source property and add providerName in App.Config file

<add name="Keyname" connectionString="Data Source=DBPath\Database1.sdf" providerName="Microsoft.SqlServerCe.Client.3.5" />
0

精彩评论

暂无评论...
验证码 换一张
取 消