我正在使用vb.net开发app,所有信息都记录在ms访问中。任何人都可以教我如何从数据表中第一行的第一列和第二列获取信息,以便在文本框中显示。
这是我尝试过的代码,当然它并没有像我希望的那样工作。
Dim cmd As New OleDbCommand("Select Sum([SharkTWbySpecies]), Sum([RayTWbySpecies]) From TWbySpecies Where [OperationID] =" & TextBoxOpID4.Text, myConnection)
Dim AllSampleTW As Integer = cmd.ExecuteNonQuery()
TextBoxAllSharkSampleTW.Text = AllSampleTW.ToString()
TextBoxAllRaySampleTW.Text = AllSampleTW.ToString()
希望有人可以帮助我。谢谢!
答案 0 :(得分:0)
使用DataReader获取数据.FYI,使用参数化查询以避免sql injection
Dim cmd As New OleDbCommand("Select Sum([SharkTWbySpecies]), Sum([RayTWbySpecies]) From TWbySpecies Where [OperationID] =@val", myConnection)
cmd.Parameters.AddWithValue("@val", TextBoxOpID4.Text)
Dim dr As OleDbDataReader = cmd.ExecuteReader
If dr.HasRows Then
While dr.Read ' loop through the datareader and get values of each column
TextBoxAllSharkSampleTW.Text = dr(0)
TextBoxAllRaySampleTW.Text = dr(1)
End While
End If