當前位置:編程學習大全網 - 編程語言 - 求VB.NET2010操作ACCESS數據庫的完整代碼

求VB.NET2010操作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=F:\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=F:\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()

'SQL拼接過程中最後不需要有分號

'在賦值時,讀取字段值時必須使用在數據庫客戶端查詢時顯示的字段名

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()

'con.Close()

MsgBox("結算完成!")

'con.Open()

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

  • 上一篇:complex和complicated區別
  • 下一篇:Abb編程求職
  • copyright 2024編程學習大全網