我有一个座位预定代码,但我不知道如何将其与访问数据库链接。我将按钮用作座位,因此当选择座位时,座位会隐藏起来,因此当我选择座位时,我需要帮助。座位号显示在访问数据库中。这是我的代码:
Public Class Form1
Dim seatnumber As char
Private Sub BTNA1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNA1.Click
seatnumber = "A1"
Confirmseat()
End Sub
Private Sub BTNA2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNA2.Click
seatnumber = "A2"
confirmseat()
End Sub
Private Sub BTNA3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNA3.Click
seatnumber = "A3"
Confirmseat()
End Sub
Private Sub BTNA4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNA4.Click
seatnumber = "A4"
Confirmseat()
End Sub
Private Sub BTNA5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNA5.Click
seatnumber = "A5"
Confirmseat()
End Sub
Private Sub BTNA6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNA6.Click
seatnumber = "A6"
Confirmseat()
End Sub
Private Sub BTNB7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNB7.Click
seatnumber = "B7"
confirmseat()
End Sub
Private Sub BTNB8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNB8.Click
seatnumber = "B8"
confirmseat()
End Sub
Private Sub BTNB9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNB9.Click
seatnumber = "B9"
confirmseat()
End Sub
Private Sub BTNB10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnb10.Click
seatnumber = "B10"
confirmseat()
End Sub
Private Sub BTNB11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnb11.Click
seatnumber = "B11"
confirmseat()
End Sub
Private Sub BTNB12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnb12.Click
seatnumber = "B12"
confirmseat()
End Sub
Public Sub confirmseat()
Dim intresult As Integer
intresult = MessageBox.Show("you selected" & seatnumber, "CONFIRM", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If intresult = Windows.Forms.DialogResult.No Then
seatnumber = "NO"
Else
Select Case (seatnumber)
Case "A1"
BTNA1.Visible = False
Case "A2"
BTNA2.Visible = False
Case "A3"
BTNA3.Visible = False
Case "A4"
BTNA4.Visible = False
Case "A5"
BTNA5.Visible = False
Case "A6"
BTNA6.Visible = False
Case "B7"
BTNB7.Visible = False
Case "B8"
BTNB8.Visible = False
Case "B9"
BTNB9.Visible = False
Case "B10"
btnb10.Visible = False
Case "B11"
btnb11.Visible = False
Case "B12"
btnb12.Visible = False
End Select
MessageBox.Show("Seat" & seatnumber & "is confirmed", "confirmation")
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim seatavailability As Integer
If seatavailability <> "12" Then
BTNA1.Visible = True
BTNA2.Visible = True
BTNA3.Visible = True
BTNA4.Visible = True
BTNA5.Visible = True
BTNA6.Visible = True
BTNB7.Visible = True
BTNB8.Visible = True
BTNB9.Visible = True
btnb10.Visible = True
btnb11.Visible = True
btnb12.Visible = True
Dim i As Integer
Dim reservedseats(1) As Char
For i = 0 To 12
Select Case (reservedseats(1))
Case "BTNA1"
BTNA1.Visible = False
Case "BTNA2"
BTNA2.Visible = False
Case "BTNA3"
BTNA3.Visible = False
Case "BTNA4"
BTNA4.Visible = False
Case "BTNA5"
BTNA5.Visible = False
Case "BTNA6"
BTNA6.Visible = False
Case "BTNB7"
BTNB7.Visible = False
Case "BTNB8"
BTNB8.Visible = False
Case "BTNB9"
BTNB9.Visible = False
Case "BTNB10"
btnb10.Visible = False
Case "BTNB11"
btnb11.Visible = False
Case "BTNB12"
btnb12.Visible = False
End Select
Next
End If
End Sub
Private Sub btncontinue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncontinue.Click
Me.Close()
End Sub
结束班级
答案 0 :(得分:0)
请注意,您有很多重复的代码。您可以通过编写将按钮转发到ConfirmSeat
的单个按钮单击处理程序来简化代码:
Private Sub ClickHandler(sender As Object, e As EventArgs)
Dim seatNumber = Mid(DirectCast(sender, Button).Name, 4)
ConfirmSeat(seatNumber)
End Sub
并按如下所示编写ConfirmSeat
:
Private Sub ConfirmSeat(seatNumber As String)
Dim result As Integer
result = MessageBox.Show("you selected" & seatnumber, "CONFIRM", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If intresult = Windows.Forms.DialogResult.No Then
MessageBox.Show("No seat confirmed")
Exit Sub
End If
Dim btn As Button = Controls("btn" & seatNumber)
btn.Visible = False
MessageBox.Show($"Seat {seatNumber} is confirmed", "confirmation")
End Sub
我不太确定您在Form1_Load
中要做什么,因为Select Case
都不会被击中。无论如何,可以将处理程序附加到按钮的Click
事件,如下所示:
For Each ctrl As Control In Controls
If Not TypeOf ctrl Is Button Then Continue
If ctrl Is btnClick Then Continue
AddHandler DirectCast(ctrl, Button).Click, ClickHandler
Next
通过这种方式,我们可以讨论使用ADO.NET在您的程序中对Access数据库进行读写。
Execute*
方法在最低级别上,使用连接字符串:
Dim connectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;" &
"Data Source=" & pathToDatabase
使用OleDbConnection
连接到数据库:
Using connection AS New OleDbConnection(connectionString)
connection.Open
' do stuff here
End Using
在打开连接的情况下,您可以使用OleDbCommand
执行命令。您可以将命令与ExecuteNonQuery
方法一起使用来执行更新查询:
Dim cmdDeleteAll As New OleDbCommand("DELETE * FROM Persons", connection)
cmdDeleteAll.ExecuteNonQuery
或与ExecuteScalar
方法一起返回单个结果:
Dim cmdCount As New OleDbCommand("SELECT COUNT(*) FROM Persons", connection)
Dim count As Integer? = cmdCount.ExecuteScalar
或使用ExecuteReader
方法返回OleDbDataReader
,以对一组结果进行一次迭代-仅向前。
Using cmd As New OleDbCommand("SELECT * FROM Persons", connection),
rdr = cmd.ExecuteReader
Do While rdr.Read
MessageBox.Show($"{rdr["FirstName"]} {rdr["LastName"]}")
Loop
End Using
DataSet
OleDbDataReader
可以从一个结果集中访问一个记录。如果要从多个结果集中加载数据,或者需要将所有数据保留在内存中,则可以使用OleDbDataAdapter
从DataSet
对象将数据加载到OleDbConnection
中:
Dim ds = new DataSet();
Using connection As New OleDbConnection(connectionString)
Dim sql = "SELECT * FROM Persons"
Dim adapter = new OleDbDataAdapter(sql, connection)
adapter.Fill(ds, "Persons")
End Using
由于DataSet
可以包含多组数据,因此存在用于处理DataSets
及其DataTables
的对象层次结构:
( Source: ADO.NET Architecture )
您还可以在数据读取器上使用LINQ和LINQ方法,以从单个结果集中检索一组对象:
Dim persons As List(Of Person)
Using reader = cmd.ExecuteReader()
persons = reader.Cast<DbDataRecord>()
.Select(Function(row) New Person With {
.LastName = row["LastName"])
.FirstName = row["FirstName"]
}).ToList
End Using