快乐小屋

有时候我们像孩子一样,挥霍着我们无处安放的青春!

导航

windows mobile 数据库问题

[原创]SQL Server Mobile在WM中的应用(C#)

SQL Server Mobile 是SQL Server 2005的功能之一,下面介绍一下SQL Mobile数据库的创建。。。

1、开始—>所有程序—>Microsoft SQL Server 2005—>SQL Server Management Studio,启动SQL Server 2005;
2、启动之后你会看到一个“连接服务器”的界面,在“服务器类型”选择“SQL Server Mobile”,“数据库文件”选择“新建数据库”;
3、这时会出现“创建新的SQL Server 2005 Mobile Edition 数据库”界面,这时输入数据库文件名,包括路径,我用的是:D:\EMS.sdf,密码根据个人需要来决定是否填写,如果密码过于简单会提示你是否继续使用;
4、上面的做完之后就会返回“连接服务器”的界面,点击“连接”按钮连接数据库;
5、建表,初始化表。

注:建好数据库之后你可以单独拷贝到PDA上,也可以和程序一起打包到PDA上,我采用的是一起打包

我在这个例子中建一个简单的表:
Create table oprInfo
(
   oprid nvarchar(10) primary key,
   oprname nvarchar(20) not null,
   password nvarchar(10) not null,
);

下面来看一看怎样利用C#在WM中使用SQL Server Mobile,我是用WM 5 来做的。。。

首先要引用System.Data.SqlServerCe,这个大家应该很清楚怎么做,在这就不详细说了,

1、查询数据库
   using System.Data.SqlServerCe;
   private void button1_Click(object sender, EventArgs e)
   {
            listView1.Items.Clear();
            string connectionString = "Data Source=\\Program Files\\TestDBOfSdf\\EMS.sdf;Password=ems";
            SqlCeConnection cn = new SqlCeConnection(connectionString);
            cn.Open();
            string sql = "select * from oprInfo";
            SqlCeCommand com = new SqlCeCommand(sql, cn);

            SqlCeDataReader read = com.ExecuteReader();
            while (read.Read())
            {
                string custid = read[0].ToString();
                string company = read[1].ToString();
                string custname = read[2].ToString();
                listView1.BeginUpdate();
                ListViewItem Item = new ListViewItem();
                Item.SubItems[0].Text = custid;
                Item.SubItems.Add(company);
                Item.SubItems.Add(custname);
                listView1.Items.Add(Item);
                listView1.EndUpdate();
            }
            read.Close();
            com.Dispose();
            cn.Close();
     }
2、插入记录
        private void button1_Click(object sender, EventArgs e)
        {
            string oprid = textBox1.Text.Trim();
            string oprname = textBox2.Text.Trim();
            string password = textBox3.Text.Trim();
            if ((oprid != "") && (oprname != "") && (password!=""))
            {
                string connectionString = "Data Source=\\Program Files\\TestDBOfSdf\\EMS.sdf;Password=ems";
                try
                {
                    SqlCeConnection cn = new SqlCeConnection(connectionString);
                    cn.Open();
                    string sql = "insert into oprInfo values('"+oprid+"','"+oprname+"','"+password+"')";
                    SqlCeCommand com = new SqlCeCommand(sql, cn);
                    com.ExecuteNonQuery();
                    com.Dispose();
                    cn.Close();
                    label4.Text = "操作成功!";
                }
                catch (Exception exp)
                {
                    label4.Text = exp.ToString();
                }
                
            }
            else
            {
                label4.Text = "信息要填写完整!";
            }
        }
3、修改记录
        private void button1_Click(object sender, EventArgs e)
        {
            string connectionString = "Data Source=\\Program Files\\TestDBOfSdf\\EMS.sdf;Password=ems";
            try
            {
                SqlCeConnection cn = new SqlCeConnection(connectionString);
                cn.Open();
                string sql = "update oprInfo set oprname='pkwsh' where oprid='42001'";
                SqlCeCommand com = new SqlCeCommand(sql, cn);
                com.ExecuteNonQuery();
                com.Dispose();
                cn.Close();
                label1.Text = "操作成功!";
            }
            catch (Exception exp)
            {
                label1.Text = exp.ToString();
            }
        }
4、删除记录
        private void button1_Click(object sender, EventArgs e)
        {
            string connectionString = "Data Source=\\Program Files\\TestDBOfSdf\\EMS.sdf;Password=ems";
            try
            {
                SqlCeConnection cn = new SqlCeConnection(connectionString);
                cn.Open();
                string sql = "delete from oprInfo where oprid='42001'";
                SqlCeCommand com = new SqlCeCommand(sql, cn);
                com.ExecuteNonQuery();
                com.Dispose();
                cn.Close();
                label1.Text = "操作成功!";
            }
            catch (Exception exp)
            {
                label1.Text = exp.ToString();
            }
        }

这里只是简单的介绍一下基本的应用,大家在实际应用的过程中,可以根据自己的需要进行改动。。。。

posted on 2009-12-15 09:22  小凤  阅读(807)  评论(2编辑  收藏  举报