如何在dataview中获取链接表详细信息?

时间:2015-01-28 06:34:06

标签: vb.net

我有两个像这样的链接表

enter image description here

此处myDataset.cat_table已在表单加载时填充,用于其他目的,我通过以下脚本从dataview获取myDataset.prod_table的数据。

       Dim myDataView As New DataView
       myDataView.Table = Me.myDataSet.prod_table
             For Each myRow As DataRow In myDataView.ToTable.Rows
                        Dim myId as integer
                        Dim myCatid as integer
                        Dim myCatName as object
                        Dim myprodDetails as object
                        myId = myRow("id")
                        myCatid = myRow("catid")
                        myCatName = ?
                        myprodDetails = myRow("details")
                   Next

在Dataview方法中,是否可以获得" myCatName"从链接表?我没有寻找单独的查询(通过左连接),因为我再次加载查询。并且"合并表"我想只适用于相同的表,所以搜索其他可能的逻辑

你的忠实

Murulimadhav

2 个答案:

答案 0 :(得分:0)

希望这两个表不仅仅是数据集,但是如果它们可能是来自sql的真实数据库表,我的建议是你可以通过SQL查询轻松地操作链表的数据。 P.S - 导入以下参考文献。

Imports System.Data.SqlClient

Private strConn As String
Private sqlConn As New SqlConnection
Private sqlcmd As New SqlCommand

    Private Sub getdatatable()
        strConn = "Data Source= Server_Name;Initial Catalog= DB_Name;User ID= User_name;Password= Password"
        sqlConn = New SqlConnection(strConn)
        sqlConn.Open()
        Dim sqlcmd As New SqlCommand("SELECT p.id, c.catname, p.products, p.details FROM cat_table c, prod_table p WHERE c.catid = p.catid", sqlConn)
        Dim myreader As SqlDataReader
        myreader = sqlcmd.ExecuteReader
        myreader.Read()
        Do While myreader.HasRows
            'datagridview.Rows.Add(myreader.Item("id").ToString, myreader.Item("catname").ToString, . . . and so on as your desire table columns that you want to show on datagridview)
            Do While myreader.Read
                'datagridview.Rows.Add(myreader.Item("id").ToString, myreader.Item("catname").ToString, . . . same coding as above Do while looping)
            Loop
            myreader.NextResult()
        Loop
    End Sub

在Form_load或按钮上单击或按任意方式触发此方法。在datagridview中执行此类展示之前,您必须首先在datagridview中预定义各自的列。我希望我会帮助你一点点。

答案 1 :(得分:0)

    Dim myDataView As New DataView
    myDataView.Table = Me.myDataSet.prod_table
    For Each myRow As DataRow In myDataView.ToTable.Rows
        Dim myId As Integer
        Dim myCatid As Integer
        Dim myCatName As Object
        Dim myprodDetails As Object
        myId = myRow("id")
        myCatid = myRow("catid")
        Dim myCatPosi As Integer = cat_tableBindingSource.Find("id", myCatid)
        If myCatPosi >= 0 Then
            cat_tableBindingSourceBindingSource.Position = myCatPosi
            myCatName = cat_tableBindingSourceBindingSource.Current("title").ToString
        End If
        myprodDetails = myRow("details")
    Next