天天看点

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数据访问)