380 likes | 537 Views
.NET + SQL SERVER. SQL Server +SP4. 確定加入六個參考. DataGrid. Button2. DataGrid. SQL Server. Connection. Command. DataAdapter. DataSet. DataGrid. code. Imports System.data Imports System.Data.SqlClient.
E N D
.NET + SQL SERVER SQL Server +SP4
DataGrid Button2
DataGrid SQL Server Connection Command DataAdapter DataSet DataGrid
code Imports System.data Imports System.Data.SqlClient 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物件 ' 建立與SQL Server資料庫的連線 ' 產生connection物件 cn = New System.Data.SqlClient.SqlConnection("uid=rcjao;pwd=rcjao;database=rcjao;server=MYCHAT-57ED6516") ' 產生command物件 cmd = New System.Data.SqlClient.SqlCommand("select * from myname", cn) ' 產生dataadapter物件 da = New System.Data.SqlClient.SqlDataAdapter(cmd) ' 加入新的資料表 ' 宣告並產生datatable物件 Dim dt As System.Data.DataTable = New System.Data.DataTable("myname") da.Fill(dt) DataGrid1.DataSource = dt End Sub
部署與執行 -使用PDA 請注意PDA畫面上的訊息 程式並未真正安裝到PDA上面
建置封包檔 畫面會閃動建置封包檔
安裝封包檔 點選執行 依照機型複製.CAB檔案到PDA上
表單方法 Close - 關閉表單 Hide - 隱藏表單 Show - 顯示表單 ‘ Form1呼叫顯示Form2 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim myfrm2 As New Form2 myfrm2.Show() End Sub
.NET + SQL CE 由SQL Server下載資料
由SQL Server下載資料 Module Module1 Public strremoteconnect As String ' SQL Server連接設定 Public localconnect As String ' sdf檔案位置設定 End Module
由SQL Server下載資料 Form2 Imports System.Data.SqlServerCe Imports System.Data Imports System.Data.SqlClient 加入SQLCE參考
由SQL Server下載資料 Form1 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click ' 跳頁到FORM2 Dim myfrm2 As New Form2 myfrm2.Show() End Sub Button3
由SQL Server下載資料 Form2 設定SQL Server連接參數(strremoteconnect) 設定PDA上SDF檔案位置(localconnect) Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load strremoteconnect = "provider=sqloledb;data source=192.168.2.2;Initial Catalog=wealth1;UID=mywealth;password=mywealth" localconnect = "Data Source=\My Documents\wealth1.sdf“ End Sub
由SQL Server下載資料 Form2 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click ‘需要Imports System.IO,檢查sdf資料庫是否存在 If System.IO.File.Exists("\My Documents\wealth1.sdf") Then System.IO.File.Delete("\My Documents\wealth1.sdf") End If ' 改變游標形狀 System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor ‘--------------------------建立資料庫 Dim eng As SqlCeEngine = New SqlCeEngine(localconnect) eng.CreateDatabase() ' 恢復油標形狀 System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default MsgBox("資料庫建立成功") End Sub
由SQL Server下載資料 Form2 Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click ' 下載資料 Dim strSQL As String = "SELECT * FROM goods" Dim rda As SqlCeRemoteDataAccess = Nothing Try rda = New SqlCeRemoteDataAccess rda.InternetUrl = “http://192.168.2.2/rda/sscesa20.dll”‘ㄧ定要指定固定且確定的ip rda.LocalConnectionString = "Provider=Microsoft.SQLSERVER.OLEDB.CE.2.0;Data Source=\My Documents\wealth1.sdf" rda.Pull("goods", strSQL, strremoteconnect, RdaTrackOption.TrackingOnWithIndexes, "ErrorTable") MsgBox("資料下載成功") Catch ex As SqlCeException MsgBox(ex.Message.ToString) Finally rda.Dispose() End Try End Sub
由SQL Server下載資料 Form2 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("goods") Dim cn As New System.Data.SqlServerCe.SqlCeConnection Dim cmd As New System.Data.SqlServerCe.SqlCeCommand Dim da As New System.Data.SqlServerCe.SqlCeDataAdapter ‘開啟sdf連線 Try cn = New System.Data.SqlServerCe.SqlCeConnection("Data Source=\My Documents\wealth1.sdf") ‘使用command執行sql指令 cmd.CommandText = "SELECT * FROM goods" 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 SqlServerCe.SqlCeException MsgBox(sqlex.Message.ToString) Catch ex As Exception MsgBox(ex.Message.ToString) End Try End Sub
由SQL Server下載資料 Form2 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Close() End Sub
由SQL Server下載資料 Form2 Convert.ToString(DataGrid1(DataGrid1.CurrentRowIndex, 0)) Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click Dim localConnection As SqlCeConnection localConnection = New SqlCeConnection(localconnect) Dim insertData As String Dim cmdCreateTable As SqlCeCommand insertData = "update goods set stuid='11111' where name='abc'" 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 Server下載資料 Form2 記得sql server的欄位一定要設定成uniqueidentifier Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click Dim localConnection As SqlCeConnection localConnection = New SqlCeConnection(localconnect) Dim insertData As String Dim cmdCreateTable As SqlCeCommand ' 需要設定 SQL Server上的uniqueidentifier的RowGuid=true才可以新增 insertData = "INSERT INTO goods(name,stuid) VALUES ('222ab','333ab')" localConnection.Open() cmdCreateTable = New SqlCeCommand(insertData, localConnection) cmdCreateTable.CommandType = CommandType.Text cmdCreateTable.ExecuteNonQuery() localConnection.Close() localConnection = Nothing MsgBox("新增資料成功") End Sub
由SQL Server下載資料 Form2 Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim rda As SqlCeRemoteDataAccess = Nothing Try rda = New SqlCeRemoteDataAccess rda.InternetUrl = “http://192.168.2.2/rda/sscesa20.dll”‘ㄧ定要指定固定且確定的ip rda.LocalConnectionString = "Provider=Microsoft.SQLSERVER.OLEDB.CE.2.0;Data Source=\My Documents\wealth1.sdf" rda.Push("goods", strremoteconnect, RdaBatchOption.BatchingOn) MsgBox("資料上傳成功") Catch ex As SqlCeException MsgBox(ex.Message.ToString) Finally rda.Dispose() End Try End Sub