DML Operation in WindowsCE (Compact Edition) Device Application With sdf Database

Earlier I wrote an article on sdf file creation from Windows applications and how to move the sdf fle from a device to the application and vice versa.

I have written methods for connect() and disconnect(), respectively, that I call before and after doing any of that.

The complete class file is as in the following:

  1. public class clsDB  
  2. {  
  3.    SqlCeConnection m_Con;  
  4.    SqlCeTransaction sqlTrans;  
  5.    SqlCeCommand com;  
  6.    SqlCeEngine engine;  
  7.    /// <summary>  
  8.    /// Connect to Database  
  9.    /// </summary>  
  10.    public void Connect()  
  11.    {  
  12.       try  
  13.       {  
  14.          // ReadSetting();  
  15.          m_Con = new SqlCeConnection();  
  16.          string _DBPATH = "\\Application\\FolderName";  
  17.          // string _DBPATH = "\\Application";  
  18.          string _DBNAME = "\\Data.sdf";  
  19.          m_Con.ConnectionString = "Data Source =" + _DBPATH + _DBNAME;  
  20.          // m_Con.ConnectionString = "";  
  21.          engine = new SqlCeEngine("Data Source =" + _DBPATH + _DBNAME);  
  22.          engine.Repair(null, RepairOption.RecoverCorruptedRows);  
  23.          m_Con.Open();  
  24.          com = new SqlCeCommand();  
  25.          com = m_Con.CreateCommand();  
  26.       }  
  27.       catch (Exception ex)  
  28.       {  
  29.          throw ex;  
  30.       }  
  31.    }  
  32.    public int ExecuteQuery(string StrSql)  
  33.    {  
  34.   
  35.       int result = 0;  
  36.       try  
  37.       {  
  38.          if (m_Con.State == ConnectionState.Open)  
  39.          {  
  40.             com.CommandText = StrSql;  
  41.             result = com.ExecuteNonQuery();  
  42.   
  43.          }  
  44.       }  
  45.       catch (Exception ex)  
  46.       {  
  47.          throw ex;  
  48.       }  
  49.       return result;  
  50.    }  
  51.    public DataSet GetDataSet(string strSql)  
  52.    {  
  53.       DataSet ds = new DataSet();  
  54.       SqlCeDataAdapter da;  
  55.       try  
  56.       {  
  57.          if (m_Con.State == ConnectionState.Open)  
  58.          {  
  59.             da = new SqlCeDataAdapter(strSql, m_Con);  
  60.             da.Fill(ds);  
  61.             da.Dispose();  
  62.          }  
  63.       }  
  64.       catch (Exception ex)  
  65.       {  
  66.          throw ex;  
  67.       }  
  68.       return ds;  
  69.    }  
  70.    public void Disconnect()  
  71.    {  
  72.       try  
  73.       {  
  74.          if (m_Con != null && m_Con.State == ConnectionState.Open)  
  75.          {  
  76.             com.Dispose();  
  77.             m_Con.Close();  
  78.          }  
  79.       }  
  80.       catch (Exception ex)  
  81.       {  
  82.          throw ex;  
  83.       }  
  84.   
  85.    }  

As in the code above, you can see that everything is the same as for SQL Server. Only the class name has changed.

SqlConnection to SqlCeConnection,
SqlCommand to SqlCecommand,
SqlDataAdapter to SqlCeDataAdapter


And so on.

Now suppose if you want to select some data on Barcode_OnRead() as in the following:

  1. private void barcode1_OnRead(object sender, Symbol.Barcode.ReaderData readerData)  
  2. {  
  3.    clsDB objClsDb = new clsDB(); // make global  
  4.    objClsDb.Connect();  
  5.    string sQuery = "select * from filedata";  
  6.    DataSet dsValidate = objClsDb.GetDataSet(sQuery);  
  7.    objClsDb.Disconnect();  

If you want to update a record:

  1. private void UpdateRecords()  
  2. {  
  3.    objClsDb.Connect();  
  4.    string sUpdateBoxQuery = "update filedata set BoxId ='b1'  
  5.    where serialnumber='s1';  
  6.    int i = objClsDb.ExecuteQuery(sUpdateBoxQuery);  
  7.    objClsDb.Disconnect();  

With the use of the code above you can easily do DML operations very easily. I want to say one thing, that a WindowsCE device application is not any rocket science. It is very easy to develop and just like your Windows application development. I don't know why Microsoft does not include this option in Visual Studio 2010 and above. If anyone knows then please comment below.

I think I explained all the points. If you have any concern then please comment.


Similar Articles