我正在try 用VB.NET在Dapper中的MS Access数据库中执行顺序数据透视
我有下面的代码,但这仍然是错误的.
还有没有别的办法,请给我带路
谢谢
Public Class Form2
Dim tps As New Tableproductservice()
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.DataSource = tps.Loaddata("A")
End Sub
End Class
Public Class Tableproductservice
Private connectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\transposerowtocolumnsqlmsaccessvbnet.accdb;Persist Security Info=False"
Public Function Loaddata(CodeProduct As String) As IEnumerable(Of Tableproduct)
Dim sql = <sql>
TRANSFORM Sum(Tableproduct.[Qty]) AS SumOfQty
SELECT Tableproduct.Codeproduct AS [CodeProduct], Tableproduct.Colour AS [Colour], Sum(Tableproduct.Qty) AS [Total]
FROM Tableproduct INNER JOIN SizeProduct ON Tableproduct.Size = SizeProduct.Size
WHERE Tableproduct.Codeproduct = 'A'
GROUP BY Tableproduct.Codeproduct, Tableproduct.Colour
PIVOT SizeProduct.Size;
</sql>.Value
Using _conn = New OleDbConnection(connectionString)
Return _conn.Query(Of Tableproduct)(sql, New With {Key .CodeProduct = CodeProduct}).ToList()
End Using
End Function
End Class
Public Class Tableproduct
Public Property CodeProduct() As String
Public Property Colour() As String
Public Property Size() As String
Public Property Qty() As Integer
Public Property Total() As Integer
End Class
Public Class SizeProduct
Public Property Size() As String
Public Property Sequence() As Integer
End Class
但是datagridview中的结果没有旋转,有什么不对劲的吗,请指导我 下面是数据网格视图中的结果屏幕截图
但在vb.net中使用相同的SQL,MS Access数据库中的结果已被透视,但尚未按顺序排列. 以下是MS-Access数据库中的结果屏幕截图
样本数据:
表TableProduct
CodeProduct | colored颜色 | 大小 | 数量 |
---|---|---|---|
A | 白色 | S | 15 |
A | 黑色 | M | 20 |
A | 白色 | L | 10 |
B | 蓝色 | S | 20 |
B | 白色 | XL | 15 |
表Sizeproduct
尺寸产品 | 序号 |
---|---|
S | 1 |
M | 2 |
L | 3 |
XL | 4 |
预期结果:
Codeproduct的位置:A
CodeProduct | colored颜色 | S | M | L | 合计 |
---|---|---|---|---|---|
A | 黑色 | 20 | 20 | ||
A | 白色 | 15 | 10 | 25 |
Codeproduct的位置:B
产品代码 | colored颜色 | S | XL | 总 |
---|---|---|---|---|
B | 蓝色 | 20 | 20 | |
B | 白色 | 15 | 15 |
结果来自@OlivierJacot-Descomes的datagridview回答