从 VB.net 连接到 SQL 数据库时,“行/列不存在数据”
Posted
技术标签:
【中文标题】从 VB.net 连接到 SQL 数据库时,“行/列不存在数据”【英文标题】:"No data exists for the row/column" when connecting to SQL database from VB.net 【发布时间】:2013-11-20 13:15:45 【问题描述】:我正在尝试创建一个具有 datagridview 的程序,当用户单击视图中的单元格时,它会查看 SQL 数据库,从同一记录中的其他字段获取信息,并自动填充相应的文本表单中的框(通过操作字段名称完成)。
但是,由于某种原因,我收到一条错误消息: “InvalidOperationException 未处理” “行/列不存在数据”
这里是与这部分程序相关的代码:
Private Sub DataGridView1_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvResults.CellMouseClick
' Set values in the text boxes to both corresponding to the film.
Dim strFields() As String = "ID", "fName", "fGenre", "fSynopsis", "fAgeRating", "fActorsActresses", "fWebRating", "fNoReservations", "fWeeksRunning"
Dim Con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=ApplicationData.accdb;Persist Security Info=False;")
Con.Open() 'Open the connection
Dim Cmd As New OleDbCommand(StringBuilderCommand("*", "Films", dgvResults.CurrentCell.Value, "fName"), Con) 'Create a string by calling the StringBuilderCommand to combine the parameters together with quotes.
Cmd.CommandType = CommandType.Text
Dim Rdr As OleDbDataReader = Cmd.ExecuteReader()
Dim intCount As Integer = 4 ' Create a loop variable.
Do While Rdr.Read() Or intCount < 6 ' While this statement is 'TRUE', e.g. there is a valid record.
strResult = "txt" & strFields(intCount).Replace("f", "") 'Remove any instances of 'f', e.g. the prefix of the string.
txtActorsActresses.Text = StringBuilderCommand("*", "Films", dgvResults.CurrentCell.Value, "fName")
Me.Controls(strResult).Text = Rdr.Item(strFields(intCount)) ' Suspect the error lies here.
'Set the text-box to the correct value from the database.
'This will allow me to go through several text boxes, and grab their corresponding values from the database.
intCount = intCount + 1
'Current error is because it cannot find any data beyond the first field taken.
'I have no idea why this is. But if I change the starting intCount value, it will successfully take a different value.
Loop
Rdr.Close() 'Cleaning up.
Cmd.Dispose()
Con.Close()
WebBrowser1.Navigate(dgvResults.CurrentCell.Value.Replace(" ", ".") & ".movie.poster.new.jpg.to") 'Grab the movie poster off the internet corresponding to the films name.
End Sub
Private Function StringBuilderCommand(Field, Table, CurrentCellValue, SearchParameter)
'Creates a suitable SQL string.
Dim MyStringBuilder As New StringBuilder("SELECT ")
MyStringBuilder.Append("*") ' Append the parameter 'Field'.
MyStringBuilder.Append(" FROM ") ' Append the SQL command 'FROM'.
MyStringBuilder.Append(Table) ' Append the parameter 'Table'.
MyStringBuilder.Append(" WHERE ") ' Append the SQL command 'WHERE'.
MyStringBuilder.Append(SearchParameter) ' Append the parameter 'SearchParameter'.
MyStringBuilder.Append("=""")
MyStringBuilder.Append(CurrentCellValue) ' Append the parameter 'CurrentCellValue', representing the cell selected.
MyStringBuilder.Append("""") 'Append a quotation mark.
Return MyStringBuilder.ToString() ' Return it to the main program.
End Function
正在连接的数据库表:
在 Visual Studio 2012 Express 中的错误视图:
“dgvResults.CurrentCell.Value”的值是从数据库中提取的电影的名称(例如“为奴十二年”)。
我做错了什么?
谢谢, C.
【问题讨论】:
该错误非常具有描述性:确保您尝试访问的列/行完全存在于您的数据库中。 我认为在 Do While Rdr.Read() 或 intCount Kostas 说得好……为什么要继续阅读超出阅读范围的内容? @kostasch。不错的收获! 【参考方案1】:问题是由您传递给阅读器的 strFields(intCount) 的值引起的。它不是有效的列索引。
【讨论】:
【参考方案2】:您可能希望先在字段上循环,然后再在 DataReader()
上循环,例如:
Do While Rdr.Read()
For intCount as Integer = 4 to 6
strResult = "txt" & strFields(intCount).Replace("f", "")
txtActorsActresses.Text = StringBuilderCommand("*", "Films", dgvResults.CurrentCell.Value, "fName")
Me.Controls(strResult).Text = Rdr.Item(strFields(intCount))
Next
Loop
我删除了Dim intCount As Integer = 4
,因为for next
循环不再需要它。
【讨论】:
以上是关于从 VB.net 连接到 SQL 数据库时,“行/列不存在数据”的主要内容,如果未能解决你的问题,请参考以下文章
如何使 datagridview 实时连接到本地存储在 VB.NET 中的访问数据库?
使用 VB.Net 在 Excel 中使用 WHERE SQL 命令