未处理的异常System.Data.SqlClient.SqlException

时间:2013-12-20 23:56:55

标签: sql vb.net

我是编程新手,我正在使用Visual Studio 2013中的SQL和VB开发一个项目。我遇到的一个错误如下:

Imports System.Data
Imports System.Data.SqlClient

此错误发生在以下行:

da.Fill(ds)

根据其他信息,包括以下信息:

  

其他信息:无效的对象名称'投诉'。

我有Form名为“投诉”我错过了什么?感谢您的任何帮助,您可以提供!请找到以下代码:

Public Class DButil
    Public cs As String

    Public Function GetDataView(ByVal sql As String) As DataView
        Dim ds As New DataSet
        Dim da As New SqlDataAdapter(sql, cs)

        da.Fill(ds)

        Dim dv As New DataView(ds.Tables(0))
        Return dv
    End Function

    Public Sub New()
        Dim strPath As String = Replace(System.AppDomain.CurrentDomain.BaseDirectory, "bin\debug", "cms.mdf")
        cs = "Data Source=(LocalDB)\v11.0;"
        cs += "AttachDbFilename='C:\Users\Sean\Documents\Visual Studio 2013\Projects\349591\349591\cms.mdf';Integrated Security=True"
        cs += "Integrated Security=True;Connect Timeout=30"
    End Sub

End Class

1 个答案:

答案 0 :(得分:2)

来自您的评论

Private Sub LoadGrid() 
    Dim sql As String sql = "SELECT Table.ComplaintID, Complaint.OpenDate, Status.StatusTitle, " 
    sql += "Table.Location, Table.Description FROM Table " 
    sql += " INNER JOIN Status ON Table.StatusID = Status.StatusID ORDER BY Table.OpenDate DESC"
    grdComplaints.DataSource = DB.GetDataView(sql) 
End Sub 

您使用名为OpenDate的表中名为Complaint的字段,但此表未在sql语句的JOIN子句中列出。您需要在JOIN子句中指定此表(或从SELECT列列表中删除列)

考虑到实际的字段名称,您可能需要设置Table.OpenDateComplaint.OpenDate之间的关系

Dim sql As String sql = "SELECT Table.ComplaintID, Complaint.OpenDate, Status.StatusTitle, " 
sql += "Table.Location, Table.Description FROM Table " 
sql += "INNER JOIN Status ON Table.StatusID = Status.StatusID "
sql += "INNER JOIN Complaint ON Table.OpenDate = Complaint.OpenDate " 
sql += "ORDER BY Table.OpenDate DESC"