Am just new to vb 2010 and am trying to fetch data from access database (Employees.mdb) but am stack on how to display the data now, anybody with help. Thanks
我刚接触vb 2010,我正在尝试从访问数据库(Employees.mdb)中获取数据,但现在是如何显示数据的堆栈,任何有帮助的人。谢谢
Public Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim LoginUser As String
LoginUser = LoginForm.TextBoxID.Text ' User Login Name
DisplayLogin.Text = LoginUser ' Display now user to form
Dim db As String = "Employees.mdb"
Dim DatabasePath As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\QBook Api\data api\" + db
If My.Computer.FileSystem.FileExists(DatabasePath) Then
Dim con As New OleDb.OleDbConnection
Dim cmd As New OleDb.OleDbCommand
Dim Sql As String
Dim NumOfRows As Integer
Dim TodayDate As String = Today
con.ConnectionString = "PROVIDER = Microsoft.Jet.OLEDB.4.0; Data Source = " + DatabasePath
con.Open() ' Opening oLe Connection
' Checking database if we have todays or Searched records
Sql = "SELECT * FROM Employees INNER JOIN TimeTable ON Employees.EmployeeNo = TimeTable.RefID WHERE Dated = '" & TodayDate & "' "
cmd = New OleDb.OleDbCommand(Sql, con)
NumOfRows = cmd.ExecuteScalar()
cmd.ExecuteNonQuery()
If NumOfRows > 0 Then
' Here I need to display the data
Else
MsgBox("Sorry we don’t have Entry Logs of (" + TodayDate + ")", MsgBoxStyle.Critical, Title:="QBook - No entry found")
End If
con.Close() ' Closing Connection
Else
MsgBox("Unable to get database file in (" + DatabasePath + ")", MsgBoxStyle.Critical)
End If ' no database found
End Sub
any suggestion is appreciated and welcomed
任何建议都表示赞赏和欢迎
1 个解决方案
#1
ExecuteScalar
only returns the first column from the first row in your result set so you won't be able to display any data other than that. It's typically used if you're getting a count, sum or something that only returns one value.
ExecuteScalar仅返回结果集中第一行的第一列,因此您将无法显示除此之外的任何数据。如果您获得计数,总和或只返回一个值的东西,通常会使用它。
If you want to read and display data use ExecuteReader
(https://msdn.microsoft.com/en-us/library/979byfca(v=vs.110).aspx) instead. You can then use that to bind your result to something like a DataGridView
.
如果要读取和显示数据,请使用ExecuteReader(https://msdn.microsoft.com/en-us/library/979byfca(v=vs.110).aspx)。然后,您可以使用它将结果绑定到类似DataGridView的内容。
Dim reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
If reader.HasRows() Then
Dim dt = New DataTable()
dt.Load(reader)
dataGridView2.DataSource = dt
Else
'No log entries to display
End If
#1
ExecuteScalar
only returns the first column from the first row in your result set so you won't be able to display any data other than that. It's typically used if you're getting a count, sum or something that only returns one value.
ExecuteScalar仅返回结果集中第一行的第一列,因此您将无法显示除此之外的任何数据。如果您获得计数,总和或只返回一个值的东西,通常会使用它。
If you want to read and display data use ExecuteReader
(https://msdn.microsoft.com/en-us/library/979byfca(v=vs.110).aspx) instead. You can then use that to bind your result to something like a DataGridView
.
如果要读取和显示数据,请使用ExecuteReader(https://msdn.microsoft.com/en-us/library/979byfca(v=vs.110).aspx)。然后,您可以使用它将结果绑定到类似DataGridView的内容。
Dim reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
If reader.HasRows() Then
Dim dt = New DataTable()
dt.Load(reader)
dataGridView2.DataSource = dt
Else
'No log entries to display
End If