Vb.net 自定连接数据库命名(1、OleDbConnection 2、OleDbDataAdapter 3、OleDbCommand) 中文:连接、填充、数
编辑:lixainhui (chzy)Public Class Form1
Dim myDataSet As New DataSet '自定数据集
Dim strCom As String '自定义数据集填充代码
Dim oleDbConnection As OleDb.OleDbConnection '自定连接代码
Dim strConnect As String '自定连接代码用来存放连接数据库路经!
Private Sub oledbcon()
'自定义数据库接代码!
Dim strConnect As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\" & Me.TextBox1.Text & "\data\db1.mdb"
OleDbConnection1 = New System.Data.OleDb.OleDbConnection(strConnect)
OleDbConnection1.ConnectionString = strConnect
End Sub
Private Sub oledbdap()
'自定义数据集填充代码!
strCom = " SELECT * FROM tt"
Dim myCommand As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(strCom, OleDbConnection1)
myCommand.Fill(myDataSet, strCom)
End Sub
Dim cmd As New OleDb.OleDbCommand '自定义连接命名代码
Dim strSql As String '自定义连接命名数据库表
Private Sub oledbcom()
'自定义COM命名代码!
Dim cmd As New OleDb.OleDbCommand
strSql = "SELECT * FROM tt"
= strSql
cmd.Connection = Me.OleDbConnection1
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
oledbcon() '连接代码
oledbdap() '填充代码
Me.OleDbConnection1.Open()
Me.DataGrid1.DataSource = myDataSet
Me.DataGrid1.DataMember = strCom
'Me.DataGrid1.Select(Me.DataGrid1.CurrentRowIndex)
Me.OleDbConnection1.Close()
Catch ex As Exception
MsgBox("数据库服务器未莲连接!请查正数据库服务器IP地址")
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
oledbcon() '连接代码
Me.OleDbConnection1.Open()
oledbcom()
Dim cmdd As New OleDb.OleDbCommand(strSql, OleDbConnection1)
Dim reader As OleDb.OleDbDataReader = cmdd.ExecuteReader()
Dim hj As String
Do While reader.Read
If reader.Item("桌号") <> "" Then
hj = Val(hj) + Val(reader.Item("单价"))
End If
Loop
Me.Label1.Text = "合计金额为:" & hj & "元"
Me.OleDbConnection1.Close()
Catch ex As Exception
End Try
End Sub
End Class