1

I am writing a prototype application in Windows Mobile 6.5 device. The objective of the app is to ask user for some inputs, collect data and store into local database and on a server.

I am done with creating GUI (in C#) of the application which takes all the necessary inputs from user.

Now, I need to insert this data into local DB and upload to server DB. Both the DBs will need to synced over HTTP when user selects to do so. I have not worked on databases much, except for writing some queries to fetch data from PostgreSQL in the past in Linux environment a few years ago.

So my question is, what is the easiest way to achieve the thing I am trying to? I don't need lot of features. The data is only strings and numbers (no files, multimedia stuff etc.) What server I should install and run? What components should I use on client side?

Thanks Ashish

Ashish Vyas
  • 617
  • 1
  • 5
  • 19

1 Answers1

0

To use database on windows mobile you need Microsoft SQL Server Compact 3.5 for Windows Mobile . http://www.microsoft.com/en-in/download/details.aspx?id=8831 . You can download and install from the link given. After installation C:\Program Files\Microsoft SQL Server Compact Edition\v3.5\Devices\wce500\armv4i will have all CAB files that needs to be installed to your mobile. Install

  • sqlce.ppc.wce5.armv4i.CAB
  • sqlce.repl.ppc.wce5.armv4i.CAB

For more information on what to install refer http://msdn.microsoft.com/en-us/library/bb986876.aspx

I have written a small helper class to do all database transactions.

     public class DataBaseHelper
{
    public enum typeOfQuery
    {
        insert,
        update,
        delete,
        getScalar,
        getDataSet,
        getDataTable
    };

    private string connectionString = Program.Connection;

    public object ExecuteDatabaseQuery(string query, Dictionary<string, object> dictionary, typeOfQuery typeOfQuery)
    {
        try
        {
            using (SqlCeConnection oCon = new SqlCeConnection(connectionString))
            {
                oCon.Open();
                string oSql = query;
                using (SqlCeCommand oCmd = new SqlCeCommand(oSql, oCon))
                {
                    oCmd.CommandType = CommandType.Text;
                    if (dictionary != null)
                    {
                        if (dictionary.Count != 0)
                        {
                            foreach (KeyValuePair<string, object> pair in dictionary)
                            {
                                if (pair.Value is DateTime)
                                    oCmd.Parameters.Add(pair.Key, SqlDbType.DateTime).Value = pair.Value ?? DBNull.Value;
                                else if (pair.Value is bool || pair.Value is Boolean)
                                    oCmd.Parameters.Add(pair.Key, SqlDbType.Bit).Value = pair.Value ?? DBNull.Value;
                                else
                                    oCmd.Parameters.Add(pair.Key, SqlDbType.NVarChar).Value = pair.Value ?? DBNull.Value;
                            }
                        }
                    }
                    // check what type of query using the enums in the constants.cs file
                    if ((typeOfQuery == (typeOfQuery.insert)) || (typeOfQuery == typeOfQuery.update) ||
                        (typeOfQuery == typeOfQuery.delete))
                    {
                        return oCmd.ExecuteNonQuery();
                    }
                    else if (typeOfQuery == typeOfQuery.getDataSet)
                    {
                        SqlCeDataAdapter adapter = new SqlCeDataAdapter(oCmd);
                        DataSet dataSet = new DataSet();
                        adapter.Fill(dataSet);
                        return dataSet;
                    }
                    else if (typeOfQuery == typeOfQuery.getDataTable)
                    {
                        SqlCeDataAdapter adapter = new SqlCeDataAdapter(oCmd);
                        DataSet dataSet = new DataSet();
                        adapter.Fill(dataSet);
                        return dataSet.Tables[0];
                    }
                    else if (typeOfQuery == typeOfQuery.getScalar)
                    {
                        object returnValue = oCmd.ExecuteScalar();
                        if (returnValue == null)
                        {
                            return string.Empty;
                        }
                        else
                            return returnValue;
                    }
                }
            }
        }
        catch (SqlCeException ex)
        {
            throw;
        }
        catch (Exception ex)
        {
            throw;
        }
        finally
        {
        }
        return false;
    }
}

You can call this class as follows

    string query = @"SELECT * FROM TABLE
                        WHERE COL1 = @COL1";

        Dictionary<string, object> dictionaryToInsert = new Dictionary<string, object>();
        dictionaryToInsert.Add("@COL1", Col1Value);

        return (DataTable)new DataBaseHelper().ExecuteDatabaseQuery(query,
            dictionaryToInsert, DataBaseHelper.typeOfQuery.getDataTable);

Similarly you can query database for other purposes also. use the enum and change the query and you will get the result.

rakesh kashyap
  • 1,418
  • 22
  • 41