VS.NET 2005建立智慧裝置新專案畫面
VS.NET 2005中內含之SerialPort物件
表單物件屬性說明 編號 物件 物件名稱(Name) 屬性設定 1 Label Label1 Text=CF Reader讀卡範例 2 3 TextBox TextBox1 將Text屬性清空 4 Button Button1 Text=開啟RFID CF Reader
PDA讀取RFID讀取器訊號使用者畫面設計
設定RFID讀取器通訊埠
程式碼 主要利用是SerialPort物件開啟通訊埠COM4 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click SerialPort1.Open() ' 開啟通訊埠 MessageBox.Show("Reader啟動成功") End Sub
程式碼 主要利用是SerialPort物件接收通訊埠資料,並將讀取到的卡號指定到TextBox1中 Private Delegate Sub SetTextBoxTextDelegate(ByVal text As String) Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived Try Dim Incoming As String = SerialPort1.ReadLine() Me.Invoke(New SetTextBoxTextDelegate(AddressOf SetTextBoxText), Incoming) Catch ex As Exception MessageBox.Show("Reader讀取失敗") End Try End Sub Private Sub SetTextBoxText(ByVal text As String) Me.TextBox1.Text = text
程式碼 主要是在程式結束時將開啟的通訊埠關閉 Private Sub Form1_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Deactivate If SerialPort1.IsOpen = True Then SerialPort1.Close() ' 關閉通訊埠 End If Me.Close() End Sub
系統測試與執行 完成讀卡程式碼後,就可以實際將程式發佈到PDA上進行測試,請利用VS.NET 2005執行本專案,選擇部署位置項目中選擇PocketPC 2003裝置。此時VS.NET 2005會透過ActivSync將程式部署到PDA上並開始執行,此步驟需要一段時間。
案執行視窗選擇部署位置畫面
專案實際在PDA上執行的起始畫面
CF讀取器成功啟動畫面
CF讀取器成功讀取卡號畫面
RDA資料交換 DataGrid SQLServerCE SQL Server SDF資料庫 rfid.sdf Data Synchronization Data Source=120.109.35.153;Initial Catalog=982RFID;User Id=982rfid;Password =abcdef RDA http://120.109.32.16:88/rda3/sqlcesa30.dll
RDA資料交換 會選用行動裝置PDA通常看中的就是他的可攜性,但因為PDA的記憶容量有限,而且當PDA完全沒有電時,以往紀錄的程式與資料將隨之煙消雲散,所以往往會將資料儲存在資料庫主機上,需要透過PDA使用時才從主機上下載資料,作業完畢後再上傳到主機上。
PDA RDA環境建置所需資源列表 編號 項目名稱 用途 1 IIS Server 5.0以上 搭配RDA運作 2 RDA 3.0 SQL Server與SQL Server CE資料上下傳與比對作業 3 SQL Server 2000/2005 主機資料庫 4 SQL Server CE 2.0 PDA上資料庫
sqlcesa30.dll與sqlcerp30.dll進行註冊 RDA註冊畫面 RDA註冊成功畫面
RDA在IIS中註冊成虛擬目錄的畫面
IIS註冊成虛擬目錄的歡迎畫面
IIS設定虛擬目錄名稱的畫面
選取RDA3真正所在目錄的畫面
將RDA3目錄設定成可執行的畫面
RDA3正確執行的畫面 其次請安裝SQL Server CE 2.0 安裝完畢後您的VS.NET 2005才具備有開發SQL CE程式的能力,接下來就可以開始透過IIS、RDA3與SQL CE來達成資料在SQL CE與SQL Server間上下傳的目的。
RDA表單畫面
表單物件屬性說明 編號 物件 物件名稱(Name) 屬性設定 1 DataGrid DataGrid1 2 Button Button1 Text=直接連線 3 Button2 Text=下載 4 Button3 Text=顯示下載的資料庫 5 Button4 Text=修改 6 Button5 Text=回傳
程式碼 Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Data.Common Imports System.Data.SqlServerCe Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim cn As System.Data.SqlClient.SqlConnection '宣告connection物件 Dim cmd As System.Data.SqlClient.SqlCommand '宣告command物件 Dim da As System.Data.SqlClient.SqlDataAdapter '宣告dataadapter物件 ' 產生connection物件 cn = New System.Data.SqlClient.SqlConnection("uid=sa;pwd=xxx;database=xx;server=xx.xx.xx") ' 產生command物件 cmd = New System.Data.SqlClient.SqlCommand("select * from a01_user", cn) ' 產生dataadapter物件 da = New System.Data.SqlClient.SqlDataAdapter(cmd) ' 宣告並產生datatable物件 Dim dt As System.Data.DataTable = New System.Data.DataTable("a01_user") da.Fill(dt) DataGrid1.DataSource = dt End Sub
下載的程式碼 (I) Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim rdaOleDbConnectString As String = "Provider=SQLOLEDB;Data Source=xx.xx.xx.xx;Initial Catalog=xx;User Id=sa;Password =xx“ '初始化 RDA Dim rda As SqlCeRemoteDataAccess = Nothing Try ' 確認檔案在PDA上是否存在? If System.IO.File.Exists("\My Documents\dc.sdf") Then System.IO.File.Delete("\My Documents\dc.sdf") End If ' 改變游標形狀 System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor '--------------------------建立資料庫 Dim eng As System.Data.SqlServerCe.SqlCeEngine = New SqlCeEngine("Data Source=\My Documents\dc.sdf") eng.CreateDatabase()
下載的程式碼 (II) ‘ 恢復油標形狀 System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default MsgBox("資料庫建立成功") rda = New SqlCeRemoteDataAccess("http://XX.XX.XX.XX/RDA3/sqlcesa30.dll", "XX", "XX", "Data Source=\My Documents\dc.sdf") rda.Pull("a01_user", "SELECT * FROM a01_user", rdaOleDbConnectString, RdaTrackOption.TrackingOnWithIndexes, "ErrorTable") MsgBox("下載成功") Catch ex As Exception MsgBox(ex.Message.ToString()) Finally rda.Dispose() End Try End Sub
顯示SDF資料庫之程式碼 (I) Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim dt As System.Data.DataTable = New System.Data.DataTable("a01_user") Dim cn As New System.Data.SqlServerCe.SqlCeConnection Dim cmd As New System.Data.SqlServerCe.SqlCeCommand Dim da As New System.Data.SqlServerCe.SqlCeDataAdapter ' 開啟dc.sdf連線 Try cn = New System.Data.SqlServerCe.SqlCeConnection("Data Source=\My Documents\dc.sdf") ' 使用command執行sql指令 cmd.CommandText = "SELECT * FROM a01_user" cmd.Connection = cn da = New System.Data.SqlServerCe.SqlCeDataAdapter(cmd) da.Fill(dt)
顯示SDF資料庫之程式碼 (II) ' 在DataGrid 顯示資料 DataGrid1.Visible = False DataGrid1.DataSource = dt DataGrid1.Visible = True cn.Close() cn = Nothing Catch sqlex As SqlServerCe.SqlCeException MsgBox(sqlex.Message.ToString) Catch ex As Exception MsgBox(ex.Message.ToString) End Try End Sub
sql指令的方式修改 (I) Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim localConnection As SqlCeConnection localConnection = New SqlCeConnection("Data Source=\My Documents\dc.sdf") Dim insertData As String Dim cmdCreateTable As SqlCeCommand ' 此處請讀者自行依照您的資料表進行sql指令修改 insertData = "update a01_user set U_Name='11111' where U_ID=34”
sql指令的方式修改(II) localConnection.Open() cmdCreateTable = New SqlCeCommand(insertData, localConnection) cmdCreateTable.CommandType = CommandType.Text cmdCreateTable.ExecuteNonQuery() localConnection.Close() localConnection = Nothing MsgBox("修改成功") Call Button3_Click(0, e) '重新顯示 End Sub
上傳程式碼 (I) Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click Dim rdaOleDbConnectString As String = "Provider=SQLOLEDB;Data Source=xx.xx.xx.xx;Initial Catalog=xx;User Id=sa;Password =xx" Dim rda As SqlCeRemoteDataAccess = Nothing Try rda = New SqlCeRemoteDataAccess ' rda3所在位置 rda.InternetUrl = "http://xx.xx.xx.xx/RDA3/sqlcesa30.dll" rda.LocalConnectionString = "Data Source=\My Documents\dc.sdf“
上傳程式碼 (II) ‘ 回傳資料 rda.Push("a01_user", rdaOleDbConnectString, RdaBatchOption.BatchingOn) MsgBox("資料上傳成功") Catch ex As SqlCeException MsgBox(ex.Message.ToString) Finally rda.Dispose() End Try End Sub
sql指令的方式新增 (I) Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim localConnection As SqlCeConnection localConnection = New SqlCeConnection("Data Source=\My Documents\dc.sdf") Dim insertData As String Dim cmdCreateTable As SqlCeCommand ' 此處請讀者自行依照您的資料表進行sql指令修改 insertData = “insert into member (m_name, loginid,pwd) values (‘xxx’,’xxx’,’xxx’)”
sql指令的方式新增(II) localConnection.Open() cmdCreateTable = New SqlCeCommand(insertData, localConnection) cmdCreateTable.CommandType = CommandType.Text cmdCreateTable.ExecuteNonQuery() localConnection.Close() localConnection = Nothing MsgBox(“新增成功") Call Button3_Click(0, e) '重新顯示 End Sub
sql指令的方式新增
利用Picturebox顯示圖片 Dim bmp As New Bitmap("\My Documents\Explorer.bmp") '圖檔路徑請自設! PictureBox1.Image = bmp