【问题标题】:OLEDBCommand handle missing database rowOLEDBCommand 处理缺少的数据库行
【发布时间】:2013-08-24 09:49:54
【问题描述】:

我有 2 个表、产品和作者,作者表有一个通过 ID 与产品表相关的列,因此我在下面做了一个选择语句,以根据 ID 获取正确的行。但是,作者表中可能没有任何数据,因此产品表中没有 ID。如果是这种情况,则不会显示来自产品的信息。

所以我的问题是我该如何处理?

    Dim ID As String = Request("id")
    If String.IsNullOrEmpty(ID) Then
        Response.Redirect("/Default.aspx")
    End If

    Try
        Using conn As New OleDbConnection(strcon)
            conn.Open()
            Dim cmd As String = "SELECT * FROM tblProducts, tblPrdAuthor " & _
                                "WHERE tblProducts.ID = " & ID & " AND tblPrdAuthor.paPrdID = tblProducts.ID"
            Using da As New OleDbDataAdapter(cmd, conn)
                Dim ds As New DataSet()
                da.Fill(ds)

                'Bind to the repeater
                rptProduct.DataSource = ds
                rptProduct.DataBind()
            End Using
        End Using
    Catch ex As Exception
        Throw ex
    End Try

谢谢!

【问题讨论】:

    标签: vb.net oledbcommand


    【解决方案1】:
    Select * from table1 as A left outer join table2 as B on (A.id=B.id) where a.id=101 and b.pid=102
    

    【讨论】:

    • 也许我没有正确解释自己。对于某些产品,第二个表中可能不存在 blPrdAuthor.paPrdID,但对于其他产品,该信息将存在于表中,因此如果没有相关 ID,我想要一种仅显示产品表中的信息的方法作者表
    • 如果存在则使用left ouver join,否​​则不使用
    • 非常感谢!我最初尝试过,但使用了 INNER JOIN。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多