天天看點

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

開發環境

Visual Studio 2005 sp1

SQL Server 2005 Enterprise Edition

Windws Mobile 6.0 Professional SDK 及中文鏡像

ActiveSync 4.5

目标平台

Windws Mobile 6.0 Professional

關于資料庫

Windows Mobile移動裝置可以通過網絡直接通路SQL Server伺服器,但是因網絡環境,應用性能等原因裝置一般不直接通路SQL Server伺服器,裝置上可以部署SQL Server Mobile(SQL Server CE),直接操作裝置上的資料庫,要遠端資料操作時,可Remot DataAccess(pull和push)及合并複制以達到資料同步。是以裝置在離線時也可資料操作就是這個道理。關于資料同步在後續随筆再讨論。

DEMO示範

此demo有2個窗體,一個是通路裝置上的SQL Server Mobile資料庫,一個是通路伺服器上的SQL Server資料庫,均做簡單的資料查詢。

建立項目

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

建立一個SQL Server Mobile資料庫,右點選解決方案中的項目名/添加/建立項/資料庫檔案,會在項目中建立一個sdf資料庫檔案。

在伺服器資源管理中,建立Table,可錄入資料。

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

設計視圖,窗體放一個DataGrid,2個按鈕,一個頁面切換,一個退出

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

後端代碼

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

Code

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

public partial class Form1 : Form

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

{

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

        public Form1()

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

{

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            InitializeComponent();

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

        }

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

        private void Form1_Load(object sender, EventArgs e)

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

{

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            string sPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.ToString()); 

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            SqlCeConnection con = new SqlCeConnection("Data Source=" + sPath + "\\AppDatabase1.sdf");

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            DataSet ds = new DataSet();

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            SqlCeDataAdapter da = new SqlCeDataAdapter("select * from ebook", con);

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            da.Fill(ds);

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            dataGrid1.DataSource = ds.Tables[0]; 

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

        }

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

        private void menuItem2_Click(object sender, EventArgs e)

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

{

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            this.Close();

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

        }

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

        private void menuItem1_Click(object sender, EventArgs e)

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

{

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            SQL sql = new SQL();

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            sql.Show();

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            this.Hide();

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

        }

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

    }

生成項目,連接配接模拟器,選擇目标平台,點選“連接配接到裝置”

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

Ctrl+F5執行,彈出部署對話框,選擇目标平台,點選"部署"

程式開始部署運作,結果如下

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

可以在裝置的資料總管中看到部署的檔案,在Program Files\項目名\一個exe檔案,一個sdf檔案。如果Exit了,可以在資料總管中運作那個exe檔案再次啟動程式,但是如果更改了代碼就要重新部署執行了。

建立第二個頁面SQL.cs

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

Code

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

public partial class SQL : Form

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

{

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

        public SQL()

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

{

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            InitializeComponent();

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

        }

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

        private void menuItem2_Click(object sender, EventArgs e)

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

{

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            this.Close();

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

        }

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

        private void menuItem1_Click(object sender, EventArgs e)

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

{

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            Form1 f1 = new Form1();

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            f1.Show();

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            this.Hide();

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

        }

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

        private void SQL_Load(object sender, EventArgs e)

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)
Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

{

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            DataTable dt = new DataTable();

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            SqlConnection con = new SqlConnection("server=192.168.1.81;database=ez4phoneDB;uid=sa;pwd=123;");

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            SqlDataAdapter da = new SqlDataAdapter("select * from ressort", con);

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            da.Fill(dt);

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

            dataGrid1.DataSource = dt;

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

        }

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

    }

注:上面的伺服器位址必須寫真實的網絡IP位址,不能為localhost或127.0.0.1

生成,連接配接模拟器,因為要通路伺服器,是以裝置要通過ActiveSync連接配接電腦以上網,工具/設定仿真管理器/

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

此時,activesync會激活,因為隻是連接配接網絡測試,不做資料同步,點選取消即可

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

經過以上設定,如果模拟器不能上網(用IE上網測試下),可能需要設定網絡連接配接

開始/設定/連接配接/連接配接/進階/選擇網絡/選擇機關設定

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)

執行程式,效果如下,資料來自SQL Server伺服器

Windws Mobile 6.0 Professional 開發入門 (ADO.NET資料通路)