1

我正在使用 sql server 作为后端和 vb 作为前端来制作一个小型数据库,我几乎让它工作了,但是我偶然发现了这个错误:

System.Data.dll 中出现“System.Data.SqlClient.SqlException”类型的未处理异常

附加信息:无法绑定多部分标识符“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
4

1 回答 1

1

问题在这里:

Dim query As String = "select * from Books where " + colNames.SelectedValue.ToString + " LIKE " + "'%" + colValues.Text +  "%'" 

我猜这colNames是一个绑定到 aDataTable或 a的控件Dataview,所以 selectedValue 是 a DataRowView

这与此帖子列表框选定项目中的完全相同的问题给我“System.Data.DataRowView”,C# winforms。您无法设置selectedValue.ToString,因为它总是返回“System.Data.DataRowView”。您需要将所选项目转换为 DataRowView,然后您可以从中获取值。

于 2013-06-28T17:14:48.087 回答