[原創]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(); } }
這裡只是簡單的介紹一下基本的應用,大家在實際應用的過程中,可以根據自己的需要進行改動。。。。 |