我只是想问这个问题(标题)。我有一个名为PR_rating的列,我需要根据学生ID向TextBoxes显示值,这样一个学生ID =很多PR_rating。我的问题是我不知道该怎么做。 TextBox只显示一个TextBox中的所有值。抱歉我的英语不好。
Imports System.Data.OleDb
Public Class adminEval
Dim provider As String
Dim dataFile As String
Dim connString As String
Public myConnection As OleDbConnection = New OleDbConnection
Public dr As OleDbDataReader
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
dataFile = "C:\Users\Billy\Desktop\TH\exampleData.accdb"
connString = provider & dataFile
myConnection.ConnectionString = connString
End Sub
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
myConnection.Open()
Dim str As String
str = "SELECT * FROM [Transaction] WHERE (Stud_no ='" & txtstudnum.Text & "')"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
While dr.Read()
txteng11.Text &= dr(7).ToString() & Environment.NewLine
答案 0 :(得分:1)
使用文本框以外的其他内容来存储此数据可能更好。我更喜欢将代码放入datatable
,然后将其加载到datagridview
。
以下是使用您的代码的示例:
'new variables added for this example
dim dataAdapter as New OldDbDataAdapter
dim dataTable as New DataTable
dim dataGrid as New DataGridView
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
myConnection.Open()
Dim str as String
str = "SELECT * FROM [Transaction] WHERE (Stud_no=@studentNum)"
Dim cmd as OleDbCommand = New OleDbCommand(str, my connection)
'Use parameters to safely fill the SQL string
cmd.Parameters.Add("@studentNum",OleDbType.String).value = txtstudnum.Text
dataAdapter.SelectCommand = cmd
'fill the dataTable using the dataAdapter, which has been executed above
dataAdapter.Fill(dataTable)
'Use the dataTable as the dataGrid's source to add the data into it
dataGrid.DataSource = dataTable
myConnection.Close()
End Sub
上面我也使用了OleDbCommand parameters。这允许您安全地填充SQL字符串以供执行。
使用Data Adapter,您可以从命令中将数据放入此中,并使用此数据填充dataTable
。填好表后,您可以将表格dataGridView
填入表单(从工具箱插入表单),将表格设置为DataSource
。
如果这不适合您的需求,我很抱歉,我通常会使用MySQL,所以OleDB对我来说很新,但语法相似。