Search code examples
c#visual-studio-2008sql-server-cewindows-cewindows-embedded-compact

Window embedded CE connectivity with SQL Compact in Visual Studio 2008


Solution Explorer is shown in this imageHow can i connect with SQL Compact server database for window CE devices. I have tired it with a simple code.Window CE should connect to the SQL compact server but i am novice user for this technology .This is error on device

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using System.Data.SqlServerCe;

namespace SQLCompactConnectivity
{
    public partial class Form1 : Form
    {
        //public SqlCeConnection con = new SqlCeConnection(@"Data Source=C:\Users\Administrator\Documents\Users.sdf");

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String query = "Select * from Instrument";
            String conString = @"Data Source =\Program Files\SQLCompactConnectivity\Data\Music.sdf";
            SqlCeConnection con = new SqlCeConnection(conString);
            SqlCeCommand cmd = new SqlCeCommand(query, con);
            con.Open();
            try
            {
                SqlCeDataReader rdr = cmd.ExecuteReader();
                try
                {
                    while (rdr.Read())
                    {
                        this.label1.Text += string.Format("\r\n ID: {0} Name: {1}", rdr[0].ToString(), rdr[1].ToString());
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    rdr.Close();
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }
    }
} 

Solution

  • After some R & D i found the answer of this question. The Error means that DLL is not compatible with the device. SQL Compact service pack 1 don't support this. The solution for this problem is to [install SQL Compact service pack 2]1 with Visual Studio 2008. The file is at C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v3.5\Devices\System.Data.SqlServerCe.dll . So Now Remove your previous System.Data.SqlServerCe.dll from reference and add service pack 2 DLL file e.g System.Data.SqlServerCe.dll. I hope this will work for all of you.