根据来自 vb.net 中另一个 SQL 服务器 table 的显示行显示列
Display column based on the displayed row from another SQL Server table in vb.net
如何在VB.NET中将行数据值显示为列名?
给定一些 table 结构。
Table 1 及其原始列和示例数据:
RECEIPTNO PIZZA NOODLES BURGER BEVERAGES
--------------------------------------------------
100 35 0 12 5
Table 2 其中只有 2 行数据:
FOODNAME FOODTYPE
------------------
NOODLES ASIAN FOOD
BEVERAGES BEVERAGES
预计 Table 1 将在 table 处的每一行显示食物名称 2.
RECEIPTNO NOODLES BEVERAGES
------------------------------
100 0 5
首先,我从 Table2 下载以从 Table1 中获取您想要的列。遍历结果构建 SELECT 语句。
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim dt As New DataTable
Dim dt2 As New DataTable
Using cn As New SqlConnection("Your connection string")
Using cmd As New SqlCommand("SELECT FOODNAME FROM Table2;", cn)
cn.Open()
dt.Load(cmd.ExecuteReader)
cn.Close()
End Using
Dim sqlSelect = "SELECT RECEIPTNO"
For Each row As DataRow In dt.Rows
sqlSelect &= "," & row("FOODNAME").ToString
Next
sqlSelect &= " FROM Table1;"
Using cmd As New SqlCommand(sqlSelect, cn)
cn.Open()
dt2.Load(cmd.ExecuteReader)
End Using
End Using
DataGridView1.DataSource = dt2
End Sub
如何在VB.NET中将行数据值显示为列名?
给定一些 table 结构。
Table 1 及其原始列和示例数据:
RECEIPTNO PIZZA NOODLES BURGER BEVERAGES
--------------------------------------------------
100 35 0 12 5
Table 2 其中只有 2 行数据:
FOODNAME FOODTYPE
------------------
NOODLES ASIAN FOOD
BEVERAGES BEVERAGES
预计 Table 1 将在 table 处的每一行显示食物名称 2.
RECEIPTNO NOODLES BEVERAGES
------------------------------
100 0 5
首先,我从 Table2 下载以从 Table1 中获取您想要的列。遍历结果构建 SELECT 语句。
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim dt As New DataTable
Dim dt2 As New DataTable
Using cn As New SqlConnection("Your connection string")
Using cmd As New SqlCommand("SELECT FOODNAME FROM Table2;", cn)
cn.Open()
dt.Load(cmd.ExecuteReader)
cn.Close()
End Using
Dim sqlSelect = "SELECT RECEIPTNO"
For Each row As DataRow In dt.Rows
sqlSelect &= "," & row("FOODNAME").ToString
Next
sqlSelect &= " FROM Table1;"
Using cmd As New SqlCommand(sqlSelect, cn)
cn.Open()
dt2.Load(cmd.ExecuteReader)
End Using
End Using
DataGridView1.DataSource = dt2
End Sub