将第二个数据表附加到现有数据表

时间:2015-06-17 14:44:45

标签: sql asp.net vb.net

我要做的是修改这段代码,这样我就可以获取第二个数据表来附加,如何附加第二个数据表的结果?

        Dim myDataTable As New DataTable
        Dim sqlConnection1 As New SqlConnection("connectionstring")
        Dim cmd As New SqlCommand
        Dim reader As SqlDataReader

        Dim Family_id = lbFamilies.SelectedItem.Value
        cmd.CommandText = "SELECT * FROM table WHERE Family_id=@Family_id"
        cmd.Parameters.AddWithValue("@family_id", Family_id)

        cmd.Connection = sqlConnection1

        sqlConnection1.Open()
        reader = cmd.ExecuteReader()
        myDataTable.Load(reader)
        sqlConnection1.Close()
        lbProduct.DataSource = myDataTable
        lbProduct.DataTextField = "product_name"
        lbProduct.DataValueField = "product_id"
        lbProduct.DataBind()

1 个答案:

答案 0 :(得分:0)

假设两者具有相同的架构,请尝试此操作。

替换:

lbProduct.DataSource = myDataTable
lbProduct.DataTextField = "product_name"
lbProduct.DataValueField = "product_id"
lbProduct.DataBind()

有了这个:

CType(lbProduct.DataSource,DataTable).Merge(myDataTable);
lbProduct.DataBind();