所以我写了这段代码......
Public Class PrikazUgovora
Private myConn As SqlConnection
Private myCmd As SqlCommand
Private Sub btnPretraziUgo_Click(sender As System.Object, e As System.EventArgs) Handles btnPretraziUgo.Click
Dim tblKorisnik As New DataTable
myConn = New SqlConnection(Pom.GetConnString())
myCmd = myConn.CreateCommand
myCmd.Parameters.AddWithValue("KliUgoPick", DateTimeUgovora.Value)
myCmd.CommandText = "select * from klijent"
Try
myConn.Open()
Dim reader As SqlClient.SqlDataReader = myCmd.ExecuteReader()
'test to confirm data received
Dim dt = New DataTable()
reader.Read()
dt.Load(reader)
DataGridViewUgo.DataSource = dt
DataGridViewUgo.AutoGenerateColumns = True
DataGridViewUgo.Refresh()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error")
Finally
If myConn.State = ConnectionState.Open Then
myConn.Close()
End If
End Try
End Sub
End Class
问题是我有五条记录,但由于某些奇怪的原因,我的gridview只填充了四个,从表中首先没有显示ID = 1的那个
如果我从klijnet写了select *,kli_id = 1 ...
,就会发生同样的事情有趣的是,在sql这个查询的工作就像一个魅力......
答案 0 :(得分:0)
最可能的原因是您正在执行读取第一条记录的reader.Read()
。然后你试着填充数据表..读者已经读过第一个。我想你可能会在这里混淆一些对象。
将其更新为以下内容:
Public Class PrikazUgovora
Private myConn As SqlConnection
Private myCmd As SqlCommand
Private Sub btnPretraziUgo_Click(sender As System.Object, e As System.EventArgs) Handles btnPretraziUgo.Click
Dim tblKorisnik As New DataTable
myConn = New SqlConnection(Pom.GetConnString())
myCmd = myConn.CreateCommand
myCmd.Parameters.AddWithValue("KliUgoPick", DateTimeUgovora.Value)
myCmd.CommandText = "select * from klijent"
Try
myConn.Open()
Dim adapter As SqlClient.SqlDataAdapter
adapter.SelectCommand = New SqlCommand(myCmd.CommandText , connection)
adapter.Fill(tblKorisnik)
DataGridViewUgo.DataSource = tblKorisnik
DataGridViewUgo.AutoGenerateColumns = True
Catch ex As Exception
MessageBox.Show(ex.Message, "Error")
Finally
If myConn.State = ConnectionState.Open Then
myConn.Close()
End If
End Try
End Sub
End Class
在SQL数据适配器上查看此信息: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2