我正在创建一个小型数据库,使用sql server作为后端,vb作为前端,我几乎使它工作但是我偶然发现了这个错误:
类型'System.Data.SqlClient.SqlException'的未处理异常 发生在System.Data.dll
中附加信息:多部分标识符 无法绑定“System.Data.DataRowView”。
这是我的代码:
Imports System.Data.SqlClient
Public Class searchDialog
Private Sub searchDialog_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'SearchDataSet.Books' table.
'You can move, or remove it, as needed.
Me.BooksTableAdapter.Fill(Me.SearchDataSet.Books)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim ds As New DataSet
Dim query As String = "select * from Books where " + colNames.SelectedValue.ToString + " LIKE " + "'%" + colValues.Text + "%'"
BooksTableAdapter.Connection.Open()
Dim adp As New SqlDataAdapter(query, BooksTableAdapter.Connection.ConnectionString)
adp.Fill(ds, "Books")
BooksTableAdapter.Connection.Close()
filteredRecords.DataSource = ds
filteredRecords.DataMember = "Books"
End Sub
End Class
答案 0 :(得分:1)
问题出在这里:
Dim query As String = "select * from Books where " + colNames.SelectedValue.ToString + " LIKE " + "'%" + colValues.Text + "%'"
我猜colNames
是一个绑定到DataTable
或Dataview
的控件,因此selectedValue是DataRowView
。
这篇文章listbox selected item give me " System.Data.DataRowView" , C# winforms中的问题完全相同。
您无法设置selectedValue.ToString
,因为它将始终返回“System.Data.DataRowView”。
您需要将所选项目转换为DataRowView,然后您可以从中获取值。