【问题标题】:Column does not belong to table Table/Cannot find table 0列不属于表 Table/找不到表 0
【发布时间】:2013-08-30 18:46:47
【问题描述】:

我的小型 Web 应用程序出现问题。当两个用户同时添加记录和更新时。

出现错误“列不属于表 Table 有时找不到表 0。”我不知道从哪里开始。请帮我。我已经创建了一个库,就在这里。

我的 ApplicationTool 类。

Private oSqlConnection As SqlConnection
Private oSqlDataAdapter As SqlDataAdapter
Private oSqlCommand As SqlCommand
Private oSqlTransaction As SqlTransaction
Public Dr As DataRow
Public Ds As DataSet

Private _strCommand As String

Public Property strCommand() As String
    Get
        Return _strCommand
    End Get

    Set(ByVal value As String)
        If Not InTransaction Then RollBack_Transaction()
        _strCommand = "SET DATEFORMAT mdy " & vbCrLf & value
        DbQuery()
    End Set
End Property

Protected Sub DbQuery()

    If Not InTransaction Then
        RollBack_Transaction()

        oSqlCommand = New SqlCommand(_strCommand, oSqlConnection)
    Else
        oSqlCommand = oSqlConnection.CreateCommand
        If _InTransaction_Initial Then
            oSqlConnection.Open()
            oSqlTransaction = oSqlConnection.BeginTransaction(IsolationLevel.ReadCommitted)
            _InTransaction_Initial = False
        End If

        oSqlCommand.Transaction = oSqlTransaction
        oSqlCommand.CommandText = _strCommand
        oSqlCommand.CommandTimeout = 0
    End If

    oSqlDataAdapter = New SqlDataAdapter(oSqlCommand)
    Ds = New DataSet
    oSqlDataAdapter.Fill(Ds)

End Sub

Private _InTransaction_Initial As Boolean = False
Private InTransaction As Boolean

Public Sub StartTransaction()
    _InTransaction_Initial = True
    InTransaction = True
End Sub
Public Sub RollBack_Transaction()
    Try
        oSqlTransaction.Rollback()
    Catch ex As Exception
    End Try
    Try
        oSqlConnection.Close()
    Catch ex As Exception
    End Try

    InTransaction = False
End Sub

Public Sub Commit_Transaction()
    Try
        oSqlTransaction.Commit()
    Catch ex As Exception
    End Try
    Try
        oSqlConnection.Close()
    Catch ex As Exception
    End Try

    InTransaction = False
End Sub

Function dsCount() As Long
    Return Ds.Tables(0).Rows.Count
End Function

Public Sub New()
    oSqlConnection = New SqlConnection("initial catalog=" & MainDb & "; data source=" & ServerName & "; user id=" & SqlID & "; password=" & SqlPassword & "; Application Name=APPTech-MIMS-Web; Connection Timeout=30")
    Dim oString As String = oSqlConnection.ConnectionString
    FileDbName = GetConfigKey("FileDb")
End Sub

我的文档类。

Public Class oDocuments
Inherits ApplicationTool


Public AppId As Long = -1 
Public AppType As oAppTypes = Nothing
Public DocNum As String

Public DocDate As DateTime
Public RefNum As String
Public PriceList As Long
Public CardCode As String
Public CardName As String
Public WhsCode As String
Public ToWhsCode As String
Public BinCode As String
Public ToBinCode As String

Public DocStatus As oDocStatuses = oDocStatuses.New_Record

Public SubmittedDate As DateTime
Public SubmittedBy As Long
Public CreatedBy As Long = GetUSER_ID

Public Lines As New oDocuments_Lines
Public Distribution As New oDocuments_Distribution

Private oDistribution As Boolean = False


Sub New(Optional ByVal oAppType As oAppTypes = Nothing, Optional ByVal oSerial As Boolean = False)
    If Not IsNothing(oAppType) Then AppType = oAppType
    Lines.Apptype = AppType
    Distribution.AppType = AppType

    If oSerial Then
        oDistribution = True
    End If
End Sub

Function ValidateData() As Boolean
    Try

        If IsNothing(AppType) Or AppType = 0 Then SetMessage("Invalid application type.", oMessageTypes.oError) : Return False

        Return True
    Catch ex As Exception
        SetMessage(ex)
    End Try
    Return False
End Function

Private oDs As DataSet
Public ReadOnly Property Count() As Integer
    Get
        Return oDs.Tables(0).Rows.Count
    End Get
End Property

Function GetRecordSet() As Data.DataTable
    Return Ds.Tables(0)
End Function

Private Function SetVariables() As Boolean
    oDs = Ds.Copy
    Dim hasData As Boolean = False

    If dsCount() > 0 Then
        AppId = GetDs("AppId") 
        AppType = GetDs("AppType")

        DocNum = GetDs("DocNum")
        DocDate = GetDs("DocDate")
        PriceList = GetDs("PriceList")
        WhsCode = GetDs("WhsCode")
        ToWhsCode = GetDs("ToWhsCode")
        BinCode = GetDs("BinCode")
        ToBinCode = GetDs("ToBinCode")
        RefNum = GetDs("RefNum")
        CardCode = GetDs("CardCode")
        CardName = GetDs("CardName")

        DocStatus = GetDs("DocStatus")

        SubmittedDate = IIf(IsDBNull(GetDs("SubmittedDate")), Now, GetDs("SubmittedDate"))
        SubmittedBy = GetDs("SubmittedBy")
        CreatedBy = GetDs("CreatedBy")

        hasData = True
    End If

    Return hasData
End Function


Function GetByAppId(ByVal oAppId As Long) As Boolean
    Try
        strCommand = "SELECT Top 1 * FROM " & GetTableName(AppType, oTableLevel.Header) & "(nolock) WHERE AppId = '" & oAppId & "'"

        Return SetVariables()
    Catch ex As Exception
        SetMessage(ex)
    End Try
    Return False
End Function

Function Add() As Boolean
    Try
        If Not ValidateData() Then Return False

        GetNextNumber(AppType)
        AppId = GetNewAppId

        If Not Update Then
            Return False

        End If

        Return True
    Catch ex As Exception
        SetMessage(ex)
    End Try
End Function

Function Update() As Boolean
    Try
        If Not ValidateData() Then Return False

        strCommand = "UPDATE " & GetTableName(AppType, oTableLevel.Header) & " SET " & _
                    "DocNum = '" & TrimData(DocNum) & "', " & _
                    "DocDate = '" & TrimData(DocDate) & "', " & _
                    "RefNum = '" & TrimData(RefNum) & "', " & _
                    "PriceList = '" & TrimData(PriceList) & "', " & _
                    "WhsCode = '" & TrimData(WhsCode) & "', " & _
                    "ToWhsCode = '" & TrimData(ToWhsCode) & "', " & _
                    "BinCode = '" & TrimData(BinCode) & "', " & _
                    "ToBinCode = '" & TrimData(ToBinCode) & "', " & _
                    "CardCode = '" & TrimData(CardCode) & "', " & _
                    "CardName = '" & TrimData(CardName) & "', " & _
                    "ModifiedDate = GETDATE(), " & _
                    "ModifiedBy = '" & GetUSER_ID & "' " & _
                    "WHERE AppId = '" & AppId & "' "

        Return True
    Catch ex As Exception
        SetMessage(ex)
    End Try
End Function

Function Submit() As Boolean
    Try
        If Not ValidateAppId(AppId) Then Return False

        Return ChangeDocStatus(AppType, AppId, oDocStatuses.Submitted)
    Catch ex As Exception
        SetMessage(ex)
    End Try
    Return False
End Function

结束类

在我添加或更新数据时的代码后面。

私有函数 SaveDocument() 作为布尔值 将 oDocument 调暗为新 WEB_Library.oDocuments(AppType.Text)

    With oDocument
        .AppId = AppId.Text
        .DocNum = AppId.Text
        .RefNum = RefNum.Text
        .PriceList = -1
        .WhsCode = WhsCode.Text

        .BinCode = BinCode.Text
        .CardCode = CardCode.Text
        .CardName = CardName.Text
        .ToWhsCode = ToWhsCode.Text

        If AppId.Text = "-1" Then
            If .Add Then
                AppId.Text = .GetNewAppId
                DocNum.Text = .GetNewAppId

                Return True
            End If

        Else
            If .Update Then
                Return True

            End If

        End If
    End With
End Function

此代码用于调用记录。

将 oDocument 作为新的 WEB_Library.oDocuments(AppType.Text)

        With oDocument
            If .GetByAppId(DocNum.Text) Then
                AppId.Text = .AppId
                DocNum.Text = .AppId
                DocDate.Text = .DocDate
                WhsCode.Text = .WhsCode
                BinCode.Text = .BinCode
                CardCode.Text = .CardCode
                CardName.Text = .CardName
                ToWhsCode.Text = .ToWhsCode
                DocStatus.Text = .DocStatus
                PriceList.Text = .PriceList
                ToWhsCode.Text = .ToWhsCode
                ToBinLoc.Text = .ToBinCode
                RefNum.Text = .RefNum
            Else
                DocDate.Text = ""
                WhsCode.Text = "-1"
                BinCode.Text = "-1"
                CardCode.Text = "-1"
                CardName.Text = "-1"
                ToWhsCode.Text = "-1"
                DocStatus.Text = "-1"
                PriceList.Text = "-1"
                ToWhsCode.Text = "-1"
                ToBinLoc.Text = "-1"
                RefNum.Text = ""
            End If

        End With

        Select Case DocStatus.Text = "-1"
            Case -1
                cmdSubmit.Visible = True

            Case 2
                cmdSubmit.Visible = False

        End Select

        Call SetGrid()

然后在 Grid 中绑定。

Sub SetGrid()
    Try
        Dim oDocument As New WEB_Library.oDocuments(AppType.Text)
        gMain.DataSource = Nothing
        gMain.DataBind()


        If oDocument.GetByAppId(AppId.Text) Then

            gMain.DataSource = oDocument.Lines.GetRecordSet.DefaultView
            gMain.DataBind()

            Dim cmbItemCode As New DropDownList
            cmbGetItemCode(cmbItemCode)

            Dim dgObj As DataGrid = gMain

            For Each oGridItem In dgObj.Items
                Dim ItemCode As DropDownList = oGridItem.FindControl("ItemCode")
                If oDocument.Lines.GetByLineId(dgObj.DataKeys(oGridItem.ItemIndex)) Then
                    Dim InvQty As AppTextBox = oGridItem.FindControl("InvQty")

                    WebCtrl.cmbCopyDataBind(cmbItemCode, ItemCode)
                    WebCtrl.cmbFocusValue(ItemCode, oDocument.Lines.ItemCode)

                End If
            Next
        End If
    Catch ex As Exception
        WebCtrl.SetMessage(ex, WEB_Library.ApplicationTool.oMessageTypes.oError)
    End Try
End Sub

希望听到积极的回应。我在我的所有 WEB 应用程序中都在努力解决这个问题。 :(

谢谢。

【问题讨论】:

  • 一个好的起点是找出发生此错误的确切行。请写下确切的错误消息。此外,考虑一下它的动机可能是一件好事(你有表 0 吗?)。
  • @varocarbas,是的,我有表 0。它在我的查询中。确切的错误是找不到表 0。接下来什么都没有。 :(
  • 没有看你的代码(这是一个相当大的);但是您问的是如何开始,这就是我的评论的内容:首先,找到错误(行和类型)。您已经有了一个答案,这很可能会有所帮助。尽管请记住,这(编写大代码并只是说“解决问题”)在这里并不完全正确;您至少应该自己找出触发错误的行...
  • @varocarbas,感谢您的回复。调用 strCommand 函数时出现错误。它有时不返回任何表。但如果只有一个用户,则不会出现错误。 >.
  • 没有strCommand函数!?您只有一个属性调用 strCommand,它存储给定的连接字符串(什么都不说)。找到 EXACT LINE 非常简单:打开 VS,查看错误出现在哪里,然后逐步重新运行此部分,直到找到确切的行。无论如何,如前所述,我没有回复您(只是写了一个关于您应该如何进行的一般性评论);您已经得到了答案(我不确定您是否已正确测试/理解)。请对 matzone 的努力表示赞赏,并更好地与他交谈。

标签: asp.net vb.net


【解决方案1】:

New DataSet 分配给数据集变量时可能会发生这种情况

如果你只在数据集中使用 1 个表,如果你使用没有数据集的数据表会更容易..

Public tbl as New DataTable

oSqlDataAdapter.Fill(tbl)

这不会让你得到那个错误......

【讨论】:

  • 嗨@matzone,非常感谢您的回复。会试试这个。谢谢。
  • 嗨@matzone,现在我只在数据集中使用1个表。明天我会反馈你的测试结果。谢谢。
猜你喜欢
  • 1970-01-01
  • 2021-09-29
  • 1970-01-01
  • 2013-06-27
  • 2013-10-10
  • 1970-01-01
  • 2014-07-03
  • 1970-01-01
相关资源
最近更新 更多