如何从Left Outer Join Quey中检索数据到Datagridview [英] how to retrive Data from Left Outer Join Quey into Datagridview

查看:75
本文介绍了如何从Left Outer Join Quey中检索数据到Datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用左外连接从VB.net中的数据库中的两个相关表中检索数据,并在Datagridview中显示结果

how to retrive data from two related tables in a database "in VB.net" using left outer join and display the results in Datagridview

推荐答案

Sub LoadData()
     If oCn.State = ConnectionState.Closed Then
         oCn.Open()
     End If
     Dim cmd As New SqlClient.SqlCommand("SELECT T_Cat.CategoryID, T_Cat.Cat_Name, TN.estbProducerID, TN.NewProducer FROM  T_Cat LEFT OUTER JOIN TN ON T_Cat.CategoryID = TN.CategoryID", oCn)
     Dim da As New SqlClient.SqlDataAdapter(cmd)
     Dim ds As New DataSet("bpl")
     Dim i As Integer = 0
     Me.DataGridView1.Rows.Clear()
     Try
         da.Fill(ds, "bpl")
         If ds.Tables(0).Rows.Count > 0 Then
             While (i <> ds.Tables(0).Rows.Count)
                 Me.DataGridView1.Rows.Add()
                 Me.DataGridView1.Item(0, i).Value = i + 1
                 Me.DataGridView1.Item(1, i).Value = ds.Tables(0).Rows(i).Item(0).ToString
                 Me.DataGridView1.Item(2, i).Value = ds.Tables(0).Rows(i).Item(1).ToString
                 Me.DataGridView1.Item(3, i).Value = ds.Tables(0).Rows(i).Item(2).ToString
                 Me.DataGridView1.Item(4, i).Value = ds.Tables(0).Rows(i).Item(3).ToString
                 Me.DataGridView1.Item(5, i).Value = Format(Date.Today, "dd-MMM-yyyy")
                 i = i + 1
             End While
         End If
     Catch ex As Exception
         MsgBox(ex.Message)
     End Try
 End Sub


这篇关于如何从Left Outer Join Quey中检索数据到Datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆