Download presentation
Presentation is loading. Please wait.
1
.NET SQL SERVER SQL Server +SP4
2
開啟新專案
3
加入必要的參考
4
設計UI
5
DataGrid SQL Server Connection Command DataAdapter DataSet DataGrid
6
顯示資料按鈕程式碼 Private Sub Button1_Click(…..
Dim cn As System.Data.SqlClient.SqlConnection Dim cmd As System.Data.SqlClient.SqlCommand Dim da As System.Data.SqlClient.SqlDataAdapter ' 建立與SQL Server資料庫的連線 cn = New System.Data.SqlClient.SqlConnection("uid=XX;pwd=XX;database=XX;server=XX.XX.XX.XX") cmd = New System.Data.SqlClient.SqlCommand("select * from XX", cn) da = New System.Data.SqlClient.SqlDataAdapter(cmd) ' 加入新的資料表 Dim dt As System.Data.DataTable = New System.Data.DataTable(“XX") da.Fill(dt) DataGrid1.DataSource = dt End Sub
7
表單顯示與切換
8
建立新表單
9
FORM2按鈕程式碼 Close - 關閉表單 Hide - 隱藏表單 Show - 顯示表單
Private Sub Button2_Click(….. Form2.Show() End Sub
10
.NET SQL CE 由SQL Server下載資料
11
由SQL Server下載資料
12
加入模組
13
模組程式碼 Module Module1 Public strremoteconnect As String ' SQL Server連接設定 Public localconnect As String ' sdf檔案位置設定 End Module
14
由SQL Server下載資料 Form2 設定SQL Server連接參數(strremoteconnect)
設定PDA上SDF檔案位置(localconnect) Private Sub Form2_Load(……. strremoteconnect = "provider=sqloledb;data source=XX.XX.XX.XX;Initial Catalog=XX;UID=XX;password=XX" localconnect = "Data Source=\My Documents\XX.sdf" End Sub
15
在PDA建立資料庫 Form2 ' 需要Imports System.IO,檢查sdf資料庫是否存在
If System.IO.File.Exists("\My Documents\XX.sdf") Then System.IO.File.Delete("\My Documents\XX.sdf") End If ' 改變游標形狀 System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor ' 建立資料庫 Dim eng As System.Data.SqlServerCe.SqlCeEngine = New System.Data.SqlServerCe.SqlCeEngine(localconnect) eng.CreateDatabase() ' 恢復油標形狀 System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default MsgBox("資料庫建立成功")
16
由SQL Server下載資料 Form2 ' 下載資料 Dim strSQL As String = "SELECT * FROM XX"
Dim rda As System.Data.SqlServerCe.SqlCeRemoteDataAccess = Nothing Try Button1_Click(sender, e) rda = New System.Data.SqlServerCe.SqlCeRemoteDataAccess rda.InternetUrl = " rda.LocalConnectionString = "Data Source=\My Documents\dmis1.sdf" rda.Pull(“XX", strSQL, strremoteconnect, System.Data.SqlServerCe.RdaTrackOption.TrackingOnWithIndexes, "ErrorTable") MsgBox("資料下載成功") Catch ex As System.Data.SqlServerCe.SqlCeException MsgBox(ex.Message.ToString) Finally rda.Dispose() End Try
17
顯示資料 Form2 Dim dt As System.Data.DataTable = New System.Data.DataTable(“XX") Dim cmd As New System.Data.SqlServerCe.SqlCeCommand Dim cn As System.Data.SqlServerCe.SqlCeConnection Dim da As System.Data.SqlServerCe.SqlCeDataAdapter ' 開啟sdf連線 Try cn = New System.Data.SqlServerCe.SqlCeConnection("Data Source=\My Documents\XX.sdf") ' 使用command執行sql指令 cmd.CommandText = "SELECT * FROM XX" cmd.Connection = cn da = New System.Data.SqlServerCe.SqlCeDataAdapter(cmd) da.Fill(dt) ' 在DataGrid 顯示資料 DataGrid1.Visible = False DataGrid1.DataSource = dt DataGrid1.Visible = True cn.Close() cn = Nothing Catch sqlex As System.Data.SqlServerCe.SqlCeException MsgBox(sqlex.Message.ToString) Catch ex As Exception MsgBox(ex.Message.ToString) End Try
18
修改資料 Form2 Dim localConnection As System.Data.SqlServerCe.SqlCeConnection localConnection = New System.Data.SqlServerCe.SqlCeConnection(localconnect) Dim insertData As String Dim cmdCreateTable As System.Data.SqlServerCe.SqlCeCommand insertData = "update XX set XX='11111' where XX=XX" localConnection.Open() cmdCreateTable = New System.Data.SqlServerCe.SqlCeCommand(insertData, localConnection) cmdCreateTable.CommandType = Data.CommandType.Text cmdCreateTable.ExecuteNonQuery() localConnection.Close() localConnection = Nothing
19
新增資料 Form2 Dim localConnection As System.Data.SqlServerCe.SqlCeConnection localConnection = New System.Data.SqlServerCe.SqlCeConnection(localconnect) Dim insertData As String Dim cmdCreateTable As System.Data.SqlServerCe.SqlCeCommand ' 需要設定SQL Server上的uniqueidentifier的RowGuid=true才可以新增 insertData = "INSERT INTO XX (PayName,PayDescription) VALUES ('小黑','黑強')" localConnection.Open() cmdCreateTable = New System.Data.SqlServerCe.SqlCeCommand(insertData, localConnection) cmdCreateTable.CommandType = Data.CommandType.Text cmdCreateTable.ExecuteNonQuery() localConnection.Close() localConnection = Nothing MsgBox("新增資料成功")
20
上傳資料 Form2 Dim rda As System.Data.SqlServerCe.SqlCeRemoteDataAccess = Nothing Try rda = New System.Data.SqlServerCe.SqlCeRemoteDataAccess rda.InternetUrl = " rda.LocalConnectionString = "Data Source=\My Documents\XX.sdf" rda.Push(“XX", strremoteconnect, System.Data.SqlServerCe.RdaBatchOption.BatchingOn) MsgBox("資料上傳成功") Catch ex As System.Data.SqlServerCe.SqlCeException MsgBox(ex.Message.ToString) Finally rda.Dispose() End Try
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.