连接字符串与SQL Server CE数据库 [英] Connection string with an SQL Server CE database
问题描述
我试图建立在Windows CE 5.0应用程序与数据库的连接,我使用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和Settings\softdil\My Documents\Visual工作室2008\Projects\Datalogic\Datalogic
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.
我也试过用相同的结果绝对URI:
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?
可能是它的东西做的aplication正在调试(执行)在移动设备?
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.
推荐答案
在我们使用的Win CE应用程序下面来获取执行文件的完整路径:
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);
使用 StartupPath
,那么你可以添加你的数据库名称设置为路径,并把它添加到连接字符串:
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);
这篇关于连接字符串与SQL Server CE数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!