一步一步實現SQL Server Compact的RDA

來源:互聯網
上載者:User

  1。首先,如果沒有安裝IIS,需要先安裝IIS。XP使用安裝盤來安裝。

如果環境是windows 2008的IIS7,在安裝下面的時候會出現會要首先安裝(IIS 6.0) backward compatibility components的錯誤訊息,解決方案是要先安裝IIS 6 Management Compatibility,請參考:

http://support.microsoft.com/default.aspx?scid=kb;en-us;955966&sd=rss&spid=2855

 

2。下載並安裝SQL Server Compact 3.5 Service Pack 1 Server Tools。

http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=fa751db3-7685-471b-ac31-f1b150422462#filelist

 

3。配置SQL Server Compact 3.5 Service Pack 1 Server Tools。

在開始菜單程式裡運行SQL Server Compact 3.5 下面的Configure Web Synchronization Wizard(配置web同步精靈)。

  • 在訂閱類型時選擇“SQL Server Compact ”。
  • 選擇“建立一個新的虛擬目錄”。
  • 選擇“複製Sql server代理到目錄中”。
  • 在用戶端驗證嚮導時 ,選擇“對用戶端進行身分識別驗證,要求輸入使用者名稱和密碼”。
  • 在驗證方式上,選擇上“基本驗證”。注意如果對IIS伺服器代理配置為使用基本驗證或整合身分識別驗證,則使用RDA對象時必須指定 InternetLogin 和 InternetPassword 屬性。

4。在行動裝置上安裝sqlserver compact 3.5。

分為手動安裝和自動安裝。

自動安裝: 

如果使用 Microsoft Visual Studio 產生一個使用 Microsoft SQL Server Compact 3.5 (SQL Server Compact 3.5)並與 .NET 連線應用程式程式,則當您第一次在裝置上部署該應用程式時,會自動在裝置上安裝 SQL Server Compact 3.5 引擎。

 

手動安裝:

可以通過手動將 .cab 檔案複製到裝置上,在裝置上安裝 SQL Server Compact 3.5。 如果是產生本機應用程式,則必須手動複製 .cab 檔案。

請參考以下的文章: 如何選擇正確的SQL Server Compact安裝包

 

5。在SQL Server伺服器上建立資料庫mymoney供測試用,其中包含一個表record。欄位資訊是:

id        int               notnull  pk    要使用RDA,必須對錶設定主鍵

action varchar(50) notnull

cost     money        notnull

 

6。編程實現RDA。

用VS2008編寫一個行動裝置應用程式,以下是在Form中實現的代碼,以下代碼在IPAQ上運行成功。

 

using System;

 

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlServerCe;

 

namespace SmartDeviceProject1

{

    public partial class Form1 : Form

    {

        // DataSource中的n28201是IIS伺服器和Sql Server的主機名稱。

        private string sCon = @"Provider=SQLOLEDB;Data Source=n28201;"

                     + @"Initial Catalog=mymoney;"

                     + @"integrated security=SSPI;Persist Security Info=False";

 

       //test是虛擬目錄名

        private string internetUrl = @"http://n28201/test/sqlcesa35.dll";

 

        private string localConnectionString = @"Data Source=/My Documents/mymoney.sdf";

 

        //IIS使用的是基本驗證,需要提供以下Windows用的使用者名稱和密碼。

        private string internetLogin = "sa";

 

        private string internetPassword = "sa";

 

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            // First, ensure you have an empty local SQL CE database.

            if (System.IO.File.Exists(@"/My Documents/mymoney.sdf"))

            {

                System.IO.File.Delete(@"/My Documents/mymoney.sdf");

            }

 

            // Create new database.

            SqlCeEngine eng = new SqlCeEngine(localConnectionString);

            eng.CreateDatabase();

 

 

            using (SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess())

            {

 

                rda.InternetUrl = this.internetUrl;

                rda.InternetLogin = this.internetLogin;

                rda.InternetPassword = this.internetPassword;

                rda.LocalConnectionString = localConnectionString;

 

                try

                {

                    rda.Pull("record", "SELECT * FROM record",

                        sCon, RdaTrackOption.TrackingOn, "rdaRecordErrors");

                }

                catch (SqlCeException sqlCeEx)

                {

                    MessageBox.Show(sqlCeEx.ToString());

                    return;

                }

                finally

                {

                    rda.Dispose();

                }

 

                MessageBox.Show("RDA Pull Done!");

            }

        }

 

 

        private void button2_Click(object sender, EventArgs e)

        {

            using (SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess())

            {

 

                rda.InternetUrl = this.internetUrl;

                rda.InternetLogin = this.internetLogin;

                rda.InternetPassword = this.internetPassword;

                rda.LocalConnectionString = localConnectionString;

 

                try

                {

                    rda.Push("record", this.sCon);

                }

                catch (SqlCeException sqlCeEx)

                {

                    MessageBox.Show(sqlCeEx.ToString());

                    return;

                }

                finally

                {

                    rda.Dispose();

                }

            }

 

            MessageBox.Show("RDA Push Done!");

        }

 

       

        private void button4_Click(object sender, EventArgs e)

        {

            using (SqlCeRemoteDataAccess rda = new SqlCeRemoteDataAccess())

            {

 

                rda.InternetUrl = this.internetUrl;

                rda.InternetLogin = this.internetLogin;

                rda.InternetPassword = this.internetPassword;

                rda.LocalConnectionString = localConnectionString;

 

                try

                {

                    rda.SubmitSql("UPDATE record set cost=888 where id=1", this.sCon);

                }

                catch (SqlCeException sqlCeEx)

                {

                    MessageBox.Show(sqlCeEx.ToString());

                    return;

                }

                finally

                {

                    rda.Dispose();

                }

            }

 

            MessageBox.Show("RDA SubmitSQL Done!");

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            Application.Exit();

        }

 

    }

}

 

 

6。在XP上安裝ActiveSync4.5。

 

7。將行動裝置用USB串連到XP主機,使用VS2008將項目發布到行動裝置,運行成功。

pull運行成功後,在行動裝置中的mymoney.sdf中建立了rdaRecordErrors和record兩個表。


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.