0
votes

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
You may want to check the current working directory against the exe path, as if its not running from there, but just calling the exe the database wont be in .\ it could be you're running from somewhere else hence it doesnt find it.BugFinder
I manually launch the .exe from the directory I transfer through FTP (so path is something like "<root>/Debug 0.1.3/Reader.exe". But I tried multiple connection strings with sometimes a full path to another directory. Can this be caused by the SQL Server CE "engine" embedded on the device?ChuckMaurice
To be honest, havent tried it on a mobile device, but normally that would be the main issue..BugFinder
I couldn't get the working directory but I tried adding the .sdf file to the external memory. This is not ideal but not a main security problem as end users won't have the ability to browse those folders. So with this path it apparently works. Data Source=\Flash File Store\LOCAL_DB\userdb.sdf;Password=PASSWORD;Persist Security Info=TrueChuckMaurice

1 Answers

0
votes

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.