当前位置:文档之家› VB连接ACCESS数据操作增删改查实例

VB连接ACCESS数据操作增删改查实例

?Imports System.Data.OleDb
Public Class Parking

Private Sub Parking_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Now_Timer.Enabled = True
End Sub

Private Sub Now_Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Now_Timer.Tick
Now_Time_Label.Text = "当前时间:" & Date.Now
End Sub

Private Sub Enter_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Enter_Button.Click
'定义一个OLEDB连接字符串
Dim conStr As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=E:\test\test.mdb"
'实例化OLEDB连接
Dim con As OleDbConnection = New OleDbConnection(conStr)

Dim sql As New System.Text.StringBuilder
'定义数据库插入语句
sql.Append("insert into Time_billing([Car_Num],[Enter_Time])")
sql.Append("values('" & Trim(Car_Num_Text.Text) & "','" & Date.Now & "')")
'打开数据库链接
con.Open()
'定义执行命令
Dim cmd As New System.Data.OleDb.OleDbCommand(sql.ToString, con)
'执行命令
cmd.ExecuteNonQuery()
'关闭数据库链接
con.Close()
MsgBox("提交成功!")
End Sub


Private Sub Leave_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Leave_Button.Click
Dim Time_Length As Double
Dim Pack_Fee As Double
Dim Enter_time As Date
Dim Leave_time As Date
Dim conStr As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=E:\test\test.mdb"
Dim con As OleDbConnection = New OleDbConnection(conStr)
Dim selSql As New System.Text.StringBuilder
Dim inSql As New System.Text.StringBuilder
Dim upSql As New System.Text.StringBuilder
Dim delSql As New System.Text.StringBuilder
Dim dr As OleDbDataReader

con.Open()
selSql.Append("select")
selSql.Append(" Enter_Time")
selSql.Append(" from [Time_billing]")
selSql.Append("where Car_Num = '").Append(Trim(Car_Num_Text.Text)).Append("'")

Dim selcmd As New OleDb.OleDbCommand(selSql.ToString, con)

dr = selcmd.ExecuteReader()
If dr.Read() Then
Enter_time = dr("Enter_Time")
Leave_time = Date.Now
Else
MsgBox("木有数据!")
End If


Enter_Time_Text.Text = Enter_time
Leave_Time_Text.Text = Leave_time
'求时间差
Time_Length = Math.Round(DateDiff(DateInterval.Minute, Enter_time, Leave_time) / 60, 2)

Pack_Fee = Time_Length * 5
Pack_Fee_Text.Text = Pack_Fee


inSql.Append("update [Time_billing]")
inSql.Append(" set [Leave_Time] = '").Append(Trim(Leave_Time_Text.Text)).Append("'")
inSql.Append(" ,[Packing_Fee] = '").Append(Pack_Fee).Append("'")


inSql.Append("where Car_Num = '").Append(Trim(Car_Num_Text.Text)).Append("'")
Dim incom As New OleDb.OleDbCommand(inSql.ToString, con)
incom.ExecuteNonQuery()
MsgBox("结算完成!")

upSql.Append("insert into Time_billing_History([Car_Num],[Enter_Time],[Leave_Time],[Packing_Fee])")
upSql.Append(" select")
upSql.Append(" [Car_Num]")
upSql.Append(",[Enter_Time]")
upSql.Append(",[Leave_Time]")
upSql.Append(",[Packing_Fee]")
upSql.Append(" from [Time_billing]")
upSql.Append("where [Car_Num] = '").Append(Trim(Car_Num_Text.Text)).Append("'")
Dim upcom As New OleDb.OleDbCommand(upSql.ToString, con)
upcom.ExecuteNonQuery()

delSql.Append("delete")
delSql.Append(" from")
delSql.Append(" [Time_billing]")
delSql.Append(" where [Car_Num] = '").Append(Trim(Car_Num_Text.Text)).Append("'")
Dim delcom As New OleDb.OleDbCommand(delSql.ToString, con)
delcom.ExecuteNonQuery()
con.Close()
End Sub

Private Sub Clear_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clear_Button.Click
Car_Num_Text.Clear()
Enter_Time_Text.Clear()
Leave_Time_Text.Clear()
Pack_Fee_Text.Clear()
End Sub
End Class

相关主题
文本预览
相关文档 最新文档