Figure 1 移动应用举例

行业 应用
销售自动化 (SFA) 提供对产品规格和客户记录的存取。
运输(后勤) 交货时间,地点,数量和样品签名。
房地产 多列表系统服务(MLS)
医疗 患者档案(已知的敏感症状,最近的化验结果,现行的药物治疗顺序)治疗时的用药信息
咨询 耗时昂贵的跟踪,委托跟踪,发票,资源调度

Figure 2 支持内建函数
Dim strSQL As String = "SELECT CONVERT(char(50), title), _
  td_sales FROM titles;"
Dim ceCn As New SqlCeConnection("data source\ssceSample.SDF")
' create datareader to populate list box
Dim ceDr As SqlCeDataReader

Try
  ceCn.Open()
  Catch a As SqlCeException
  MsgBox(a.ToString())
End Try

Try
  Dim ceCmd As New System.Data.SqlServerCe.SqlCeCommand(strSQL, ceCn)
  'Create an instance of the ListBox.
  Dim listBox1 As New ListBox()

  With listBox1
    ' Set the size and location of the ListBox.
    .Size = New System.CeDrawing.Size(208, 160)
    .Location = New System.CeDrawing.Point(8, 64)

    ' Add the ListBox to the form
    Me.Controls.Add(listBox1)
      ceDr = ceCmd.ExecuteReader()
    While ceDr.Read()
      .Items.Add(ceDr.GetString(0))
    End While
  End With
  Catch a As SqlCeException
    MsgBox(a.ToString())
  Catch a As Exception
    MsgBox(a.ToString())
End Try

ceDr.Close()

Try
  ceCn.Close()
  Catch a As SqlCeException
  MsgBox(a.ToString())
End Try 

Figure 3 使用 Pull 方法
' create RDA object
Dim ceRda As New RemoteDataAccess()
Dim strSQL = "SELECT * FROM Customers;"
Dim strRemoteConnect = "provider=sqloledb;data source=" &_
    strDataSource & ";Initial Catalog=Northwind;user id=sa;password=sa"

Try
   ceRda.InternetUrl = strInternetURL
   ceRda.LocalConnectionString = strLocalConnect

   ' pull down FOO table
   ceRda.Pull("FOO", strSQL, _
       strRemoteConnect, _
       Data.SqlServerCe.RdaTrackOption.TrackingOn)
   Catch a As SqlCeException
     MsgBox(a.ToString())
End Try

Figure 4 连接向导


Figure 9 创建和同步数据库
private void DBInit()
{
    try
    {
        if (!System.IO.File.Exists(sSubscriptionDB))
        {
            SqlCeEngine oEng = new SqlCeEngine("data source=" + 
                               sSubscriptionDB);
            oEng.CreateDatabase();
            DBSync();
        }
    }
    catch (SqlCeException ex)
    {
        ShowErrors(ex);
    }
    catch (System.Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Figure 10 Synchronize 方法
private void DBSync()
{
    // instantiate replication object
    SqlCeReplication oRepl = new SqlCeReplication();

    try
    {
        // set publisher properties
        oRepl.Publisher = "MBLAP01";
        oRepl.PublisherDatabase = "Nwind_SQLCE";
        oRepl.Publication = "SQLCEReplDemoNet";

        // set publisher security properties
        oRepl.PublisherLogin = "sa";
        oRepl.PublisherPassword = "sa";

        // set subscriber properties
        oRepl.SubscriberConnectionString = 
            "Provider=Microsoft.SQLServer.OLEDB.CE.2.0;
            Data Source=" + sSubscriptionDB;
        oRepl.Subscriber = "MBPPC01";

        // set internet properties
        oRepl.InternetUrl = "http://mblap01/sqlce/sscesa20.dll";

        // perform full synchronization
        oRepl.ExchangeType = ExchangeType.BiDirectional;
        oRepl.Synchronize();
    }
    catch (SqlCeException ex)
    {
        ShowErrors(ex);
    }
    catch (System.Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        oRepl.Dispose();
    }
}

Figure 11 捕获错误
public static void ShowErrors(SqlCeException ex)
{
    SqlCeErrorCollection oErrors = ex.Errors;

    StringBuilder oStrBld = new StringBuilder();

    Exception oInner = ex.InnerException;

    foreach (SqlCeError oErr in oErrors)
    {
        oStrBld.Append("\nError Code: " + oErr.HResult.ToString("X"));
        oStrBld.Append("\nMessage   : " + oErr.Message);
        oStrBld.Append("\nMinor Err.: " + oErr.NativeError);
        oStrBld.Append("\nSource    : " + oErr.Source);

        foreach (int iNumPar in oErr.NumericErrorParameters)
        {
            if (iNumPar != 0)
                oStrBld.Append("\nNum. Par.  : " + iNumPar);
        }
        foreach (String sErrPar in oErr.ErrorParameters)
        {
            if (sErrPar != String.Empty)
                oStrBld.Append("\n Err. Par.  : " + sErrPar);
        }
        MessageBox.Show(oStrBld.ToString(), "SqlCeException");
        oStrBld.Remove(0, oStrBld.Length);
    }
}

Figure 12 截掉空格
private void FindEmployees()
{
    // 实例化 SqlCeConnection 对象
    SqlCeConnection oCon = new SqlCeConnection("DataSource=" + 
        sSubscriptionDB);

    try
    {
        oCon.Open();

        SqlCeCommand oCmd = oCon.CreateCommand();

        oCmd.CommandText = "SELECT EmployeeID, RTRIM(LastName) + ', ' + 
            RTRIM(FirstName) AS \"Full Name\" FROM Employees
            WHERE LastName LIKE ? ORDER BY LastName";

        // pass the search term as a parameter
        oCmd.Parameters.Add(new SqlCeParameter("p1", txtLastName.Text + 
            "%"));

        SqlCeDataReader oReader = oCmd.ExecuteReader();

        // populate the list view control
        lvwEmployees.Items.Clear();
        while (oReader.Read())
        {
            ListViewItem oItem = lvwEmployees.Items.Add(new 
                ListViewItem(new String []{oReader.GetString(1), 
                oReader.GetInt32(0).ToString()}));
        }
    }
    catch (SqlCeException ex)
    {
        ShowErrors(ex);
    }
    catch (System.Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        // close and release the connection
        if (oCon.State == ConnectionState.Open)
            oCon.Close();

        if (oCon != null)
            oCon.Dispose();
    }
}