我想在vb.net的DataGridView中只显示一条记录,但下面的代码却不能正常工作。
Dim itm As Integer
itm = InputBox("Enter Roll no of student")
Dim t As New DataTable
Dim adapter As OleDbDataAdapter = New OleDbDataAdapter()
Dim cmd As OleDbCommand
Dim reader As OleDbDataReader = Nothing
Dim tuser As String = itm
If objCon.State = ConnectionState.Closed Then
objCon.ConnectionString = strConnection
objCon.Open()
End If
Dim sql As String = "SELECT * FROM testsession WHERE ROLLNUMBER=" & itm
Try
cmd = New OleDbCommand(sql, objCon)
reader = cmd.ExecuteReader()
While reader.Read
MessageBox.Show(reader.GetString(0).ToString & _
vbTab & vbTab & reader.GetString(1).ToString)
End While
Finally
If reader IsNot Nothing Then reader.Close()
End Try
此处testsession
是我的表名,rollnumber
是我搜索的主键。
答案 0 :(得分:0)
Dim dt As New DataTable, itm = InputBox("Enter Roll no of student",, "0")
Using da As New OleDbDataAdapter("SELECT TOP 1 * FROM testsession WHERE ROLLNUMBER=" & itm, strConnection)
da.Fill(dt)
End Using
Me.DataGridView1.DataSource = dt ' change DataGridView1 to the name of your DataGridView