这段代码应该很简单。
我有8行数据库。
我正在尝试使用下面的代码来阅读它们并显示它们。
到目前为止,代码只读取7,忽略了第一行。
我做错了什么?
Protected Sub getRecords()
Dim cnn As SqlConnection
Dim cmd As SqlCommand
Dim vimeoURL As String
Dim VideoTitle As String
Dim VideoDescription As String
Dim sqlStr As String = ""
Dim connStr As String = ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionString
sqlStr = "SELECT [url], [Title], [Description] FROM [VIDEOS]"
cnn = New SqlConnection(connStr)
cnn.Open()
cmd = New SqlCommand(sqlStr, cnn)
Dim dr = cmd.ExecuteReader()
If dr.Read() Then
Do While (dr.Read())
vimeoURL = dr.GetString(0)
VideoTitle = dr.GetString(1)
VideoDescription = dr.GetString(2)
Dim literal As New LiteralControl()
literal.Text += (Convert.ToString("<a class='fancybox' data-fancybox-group='group01' title='" & VideoTitle & "' data-fancybox-type='iframe' href='#' onclick=""window.open('" & vimeoURL & "' , 'myWin', 'width=780,height=600,toolbar=no,location=no,directories=no,statu s=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes'); return false""><img src='images/thumb-vimeo-video.jpg' />'<br /><b>" & VideoTitle & "</b><br />" & VideoDescription & "</a></><br /><br />"))
div1.Controls.Add(literal)
Loop
End If
dr.Close()
答案 0 :(得分:3)
只是因为你两次调用dr.Read(),并且每当你调用它时,它就会前进到下一行。
你真的不需要调用If dr.Read()
,而while条件会照顾它。
干杯
编辑:
这是你的代码重写:
Dim connStr As String = ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionString
sqlStr = "SELECT [url], [Title], [Description] FROM [VIDEOS]"
cnn = New SqlConnection(connStr)
cnn.Open()
cmd = New SqlCommand(sqlStr, cnn)
Dim dr = cmd.ExecuteReader()
Do While (dr.Read())
vimeoURL = dr.GetString(0)
VideoTitle = dr.GetString(1)
VideoDescription = dr.GetString(2)
Dim literal As New LiteralControl()
literal.Text += (Convert.ToString("<a class='fancybox' data-fancybox-group='group01' title='" & VideoTitle & "' data-fancybox-type='iframe' href='#' onclick=""window.open('" & vimeoURL & "' , 'myWin', 'width=780,height=600,toolbar=no,location=no,directories=no,statu s=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes'); return false""><img src='images/thumb-vimeo-video.jpg' />'<br /><b>" & VideoTitle & "</b><br />" & VideoDescription & "</a></><br /><br />"))
div1.Controls.Add(literal)
Loop
dr.Close()