使用内部联接在gridview中绑定数据

时间:2014-09-11 14:36:18

标签: mysql asp.net vb.net

我正在使用mysql数据库,我正在尝试使用内部联接查询填充gridview,但我无法解决它。这是我的代码。

    Sub loadingaccess4()
    Dim adapter As New OdbcDataAdapter("Select customer.CustomerName,   customer.CustomerID from customer INNER JOIN transaction on customer.CustomerID=transaction.CustomerID", dbconn)
    Dim dataset As New DataSet

    dataset.Clear()
    adapter.Fill(dataset, "")
    Me.GridView4.DataSource = dataset.Tables(0).DefaultView
    Me.GridView4.DataBind()
End Sub

你能帮助我吗?

对不起,伙计们,好的,我要澄清一下。 gridview什么都没有显示。我的代码是错的还是什么?

2 个答案:

答案 0 :(得分:0)

试试这个:

dim dt as new DataTable();
command.CommandType = CommandType.CommandText
command.CommandText = "Select customer.CustomerName,   customer.CustomerID from customer   INNER JOIN transaction on customer.CustomerID=transaction.CustomerID"
dt.Load(command.ExecuteReader())
Me.GridView4.DataSource = dt
Me.GridView4.DataBind()

答案 1 :(得分:0)

这就是我使用的

    Dim conn2 As New SqlConnection("Your Connection string here")
    Dim command As SqlCommand
    Dim SQLReader As SqlDataReader

    conn2.Open()

    command = New SqlCommand("Select customer.CustomerName,   customer.CustomerID from customer   INNER JOIN transaction on customer.CustomerID=transaction.CustomerID", conn2)

    SQLReader = command.ExecuteReader()
    GridView4.DataSource = SQLReader 
    GridView4.DataBind()

然后使用此

将它们绑定到gridview
<asp:Label ID="lable1" runat="server" Text='<%# Bind("CustomerName") %>'></asp:Label>

希望这有帮助!