关于Gridview问题
我将从数据库中的记录选出插入到Gridview中,应该是每条记录占用一行,使用循环语句时出错,代码如下:Dim strConnect As String = "data source=FG-C99768EB468F\SQLEXPRESS;integrated security = sspi;initial catalog=高校田径运动会"
Dim sqlConnectionl As SqlClient.SqlConnection
sqlConnectionl = New System.Data.SqlClient.SqlConnection(strConnect)
Try
sqlConnectionl.Open()
MessageBox.Show("以正确建立连接", "连接正确对话框")
Catch sqlclient As SqlException
MessageBox.Show(sqlclient.Message, "连接失败对话框")
Exit Sub
End Try
Dim cmd As New SqlCommand
cmd.Connection = sqlConnectionl
= CommandType.Text
Dim str As String
str = "select * from tdzfb"
Dim adapter As New SqlDataAdapter(str, sqlConnectionl)
Dim dataset As New DataSet
Dim navigator As BindingManagerBase
dataset.Clear()
adapter.Fill(dataset, "tdzfb")
navigator = Me.BindingContext(dataset, "tdzfb")
Dim i, j As Integer
i = 0
j = navigator.Count
Do While i <= j - 1
Dim c As String = "select * from tdzfb "
= c
Dim mydatareader As SqlDataReader = cmd.ExecuteReader()
mydatareader.Read()
DataGridView2.Rows(i).Cells(0).Value = mydatareader.GetString(0) ‘执行到这时系统提示indix错误’
DataGridView2.Rows(i).Cells(1).Value = mydatareader.GetInt32(1)
mydatareader.Close()
i = i + 1
Loop
sqlConnectionl.Close()