如何在Visual Basic 2012中使用mdb数据库中的数据填充列表框?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Visual Basic 2012中使用mdb数据库中的数据填充列表框?相关的知识,希望对你有一定的参考价值。
我正在使用Visual Basic 2012制作学生档案以进行计算任务。
我有一个组合框,里面有两门课程:计算机和商务。 (在表格的右侧,附图)。此外,按下的列表按钮应根据组合框中选择的课程显示.mdb
数据库中的学生。
如何使用我的数据库中的学生填充列表框(我有)?
以下更准确的是我到目前为止的代码:
Public Class Form1 Dim co As New ADODB.Connection Dim rs As New ADODB.Recordset Dim confirm As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
co.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:DocumentsProfileDB.mdb;Persist Security Info=False")
rs.Open("Select * from ProfileTBL", co, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic)
Combo1.Items.Add("Computer Technology")
Combo1.Items.Add("Business Management")
display()
End Sub
Sub display()
TextBox1.Text = rs.Fields.Item("FirstName").Value
TextBox2.Text = rs.Fields.Item("Surname").Value
Combo1.Text = rs.Fields.Item("Course").Value
TextBox4.Text = rs.Fields.Item("Phone").Value
End Sub
Private Sub addnew_Click(sender As Object, e As EventArgs) Handles addnew.Click
rs.AddNew()
clear()
End Sub
Sub clear()
TextBox1.Text = ""
TextBox2.Text = ""
Combo1.Text = "Select Course"
TextBox4.Text = ""
End Sub
Private Sub Savebtn_Click(sender As Object, e As EventArgs) Handles Savebtn.Click
rs.Fields("FirstName").Value = TextBox1.Text
rs.Fields("Surname").Value = TextBox2.Text
rs.Fields("Course").Value = Combo1.Text
rs.Fields("Phone").Value = TextBox4.Text
MsgBox("Data is saved successfully.", vbInformation)
rs.Update()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
rs.MovePrevious()
If rs.BOF Then
rs.MoveLast()
display()
Else
display()
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
rs.MoveNext()
If Not rs.EOF Then
display()
Else
rs.MoveFirst()
display()
End If
End Sub
Private Sub deletebtn_Click(sender As Object, e As EventArgs) Handles deletebtn.Click
confirm = MsgBox("Do you want to delete the Student Profile", vbYesNo + vbCritical, "Deletion Confirmation")
If confirm = vbYes Then
rs.Delete()
MsgBox("Record has been Deleted successfully", vbInformation, "Message")
rs.Update()
Refresh()
clear()
Else
MsgBox("Profile Not Deleted!", vbInformation, "Message")
End If
End Sub
Private Sub findbtn_Click(sender As Object, e As EventArgs) Handles findbtn.Click
rs.Close()
rs.Open("Select * from ProfileTBL where Phone='" + TextBox4.Text + "'", co, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic)
If Not rs.EOF Then
display()
reload()
Else
MsgBox("Record Profile not found!", vbInformation)
End If
End Sub
Sub reload()
rs.Close()
rs.Open("Select * from ProfileTBL", co, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic)
End Sub
Private Sub listbtn_Click(sender As Object, e As EventArgs) Handles listbtn.Click
End Sub
结束班
答案
可能如下:
Private Sub btnListStudents_Click(sender As Object, e As EventArgs) Handles btnListStudents.Click
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("StudentId", GetType(Integer)),
New DataColumn("StudentName", GetType(String)),
New DataColumn("StudentCourse", GetType(Integer))})
dt.Rows.Add(1, "John Hammond", 555)
dt.Rows.Add(2, "Mudassar Khan", 666)
dt.Rows.Add(3, "Suzanne Mathews", 777)
dt.Rows.Add(4, "Robert Schidner", 888)
dt.Rows.Add(5, "John Doo", 999)
'Clear first
lstStudents.Items.Clear()
'Fill names into the listbox
For Each row As DataRow In dt.Rows
lstStudents.Items.Add(row("StudentName"))
Next row
End Sub
以上是关于如何在Visual Basic 2012中使用mdb数据库中的数据填充列表框?的主要内容,如果未能解决你的问题,请参考以下文章
如何在Visual Basic 2012中使用mdb数据库中的数据填充列表框?
如何在 Visual Basic 2010 中使用 sqlite db?
如何在 Visual Basic .NET 中找到默认音频设备?