Unable to access database (SQL Server CE / Windows Mobile 6.5)

对着背影说爱祢 提交于 2019-12-12 04:25:21

问题


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

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

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


回答1:


Windows Mobile neither knows about drive letters nor a 'current' directory. That is why "Data Source=.\userdb.sdf" will always fail.

On windows mobile you always have to use a full qualified path name to access a file.

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.



来源:https://stackoverflow.com/questions/43178828/unable-to-access-database-sql-server-ce-windows-mobile-6-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!