[英]Unable to access database (SQL Server CE / Windows Mobile 6.5)
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). 我正在为Windows Mobile 6.5设备创建一个应用程序,该应用程序的一部分应该在设备插入时复制SQL数据库,在拔出设备时具有本地副本(因为没有连接)。
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. 因此,要拥有本地数据库,我遵循了有关SQL Server CE的“罗斯文”教程 (带有我自己的测试数据库),但始终遇到设备无法连接到数据库的问题。 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 database error: Unspecified error [.\\userdb.sdf]
SQL数据库错误:未指定的错误[。\\ 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);
}
}
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 我也尝试了一些不同的方法,其中表单中填充了设计师的数据集,但仍然无法手动访问数据库: https : //pastebin.com/10P3wGeP
Windows Mobile neither knows about drive letters nor a 'current' directory. Windows Mobile既不知道驱动器号也不知道“当前”目录。 That is why "Data Source=.\\userdb.sdf" will always fail.
这就是为什么“数据源=。\\ userdb.sdf”将始终失败的原因。
On windows mobile you always have to use a full qualified path name to access a file. 在Windows Mobile上,您始终必须使用完整的限定路径名来访问文件。
You may use 您可以使用
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. 获取可执行文件启动目录的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.