我正在尝试使用 VB.NET 在 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中的结果没有旋转有什么问题请指导我 下面是 datagridview 中结果的屏幕截图
但是在 vb.net 中使用相同的 sql,MS ACCESS 数据库中的结果已被转换,但尚未按顺序执行。 下面是 MS-ACCESS 数据库中结果的屏幕截图
样本数据:
桌子
TableProduct
代码产品 | 颜色 | 尺寸 | 数量 |
---|---|---|---|
A | 白色 | S | 15 |
A | 黑色 | M | 20 |
A | 白色 | L | 10 |
B | 蓝色 | S | 20 |
B | 白色 | XL | 15 |
桌子
Sizeproduct
产品尺寸 | 顺序 |
---|---|
S | 1 |
M | 2 |
L | 3 |
XL | 4 |
期望的结果:
代码产品在哪里:A
代码产品 | 颜色 | S | M | L | 总计 |
---|---|---|---|---|---|
A | 黑色 | 20 | 20 | ||
A | 白色 | 15 | 10 | 25 |
代码产品在哪里:B
代码产品 | 颜色 | S | XL | 总计 |
---|---|---|---|---|
B | 蓝色 | 20 | 20 | |
B | 白色 | 15 | 15 |
好吧,你的
Loaddata
方法 awlays 返回 Tableproduct
对象,这是你的 datagridview 看到的唯一东西。您必须创建一个代表您的透视结果的类,并从 Loaddata
方法返回它。
Public Class PivotedProduct
Public Property CodeProduct() As String
Public Property Colour() As String
Public Property S() As Integer
Public Property M() As Integer
Public Property L() As Integer
Public Property XL() As Integer
Public Property Total() As Integer
End Class
但这当然会始终显示网格中的所有列。我不确定你所说的“顺序”是什么意思。如果您的意思是按产品分组,则无法通过
DataGridView
来实现。但有些第三方网格可以支持分组。