无法访问数据库(SQL Server CE/Windows Mobile 6.5) [英] Unable to access database (SQL Server CE / Windows Mobile 6.5)
问题描述
我正在为 Windows Mobile 6.5 设备创建一个应用程序,其中的一部分应该在设备插入时复制 SQL 数据库,在拔下时有一个本地副本(因为没有连接).
I'm creating an application for a Windows Mobile 6.5 device, part of which is supposed to replicate a SQL database when the device plugged in, to have a local copy when unplugged (because no connection).
因此,为了拥有一个本地数据库,我遵循了Northwind"教程SQL Server CE(使用我自己的测试数据库),但经常遇到设备无法连接到数据库的问题.我无法调试(因为我每次都必须将程序传输到设备进行测试)但我收到此错误消息(无论我尝试使用什么连接字符串):
So, to have a local database I followed the "Northwind" tutorial for SQL Server CE (with my own test database), but constantly run into the issue that the device can't connect to the database. I can't debug (because I have to transfer the program to the device to test it every time) but I get this error message (whatever the connection string I try to use):
SQL 数据库错误:未指定的错误 [.\userdb.sdf]
SQL database error: Unspecified error [.\userdb.sdf]
我尝试了多个连接字符串,如果我使用设计器将数据源绑定到表单,我可以看到可以访问数据库.但是我需要手动设置一些查询,因为它们比填写表格要复杂得多.
I tried multiple connection strings, and I can see the database can be accessed if I bind the data sources to the forms using the designer. But I need to manually set up some query as they will be a lot more complicated than just filling forms.
这是我的代码,它只是用来填写表格,但下一步是登录/密码检查:
Here's my code, it's just supposed to fill a form but the next step would be a login/password check:
public partial class UserLogin : Form
{
private SqlCeConnection _conn;
public UserLogin()
{
InitializeComponent();
_conn = new SqlCeConnection(@"Data Source=.\userdb.sdf;Password=PASSWORD;Persist Security Info=True");
}
private void buttonCancel1_Click(object sender, EventArgs e)
{
this.Close();
}
private void buttonLoad1_Click(object sender, EventArgs e)
{
try
{
SqlCeCommand cmd = new SqlCeCommand();
cmd.Connection = _conn;
cmd.CommandText = "SELECT userid, username, password FROM Users";
_conn.Open();
SqlCeResultSet resultSet = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable);
this.bindingSource1.DataSource = resultSet;
}
catch (SqlCeException sql_ex)
{
MessageBox.Show("SQL database error: " + sql_ex.Message);
}
}
我也尝试了一些不同的东西,其中表单填充了来自设计器的数据集,但我仍然无法手动访问数据库:https://pastebin.com/10P3wGeP
Also something different I tried, where the forms are filled with a DataSet from designer but I still can't manually access the database: https://pastebin.com/10P3wGeP
推荐答案
Windows Mobile 既不知道驱动器号,也不知道当前"目录.这就是Data Source=.\userdb.sdf"总是失败的原因.
Windows Mobile neither knows about drive letters nor a 'current' directory. That is why "Data Source=.\userdb.sdf" will always fail.
在 windows mobile 上,您始终必须使用完全限定的路径名来访问文件.
On windows mobile you always have to use a full qualified path name to access a file.
你可以使用
string path = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
获取可执行文件启动目录的路径.
to get the path to the directory of where the executable is started.
这篇关于无法访问数据库(SQL Server CE/Windows Mobile 6.5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!