大家好,我的数据库中有两个字段。 “kode_faktur”作为事务id,“kode_brg”作为item-id ..这两个字段存在于我的详细事务表中。 所以让我们说1个交易ID可以有不同数量的item-id .. 例如,在我的ss下面“kode_faktur”1有5个item-id(t1,k4,k9,c2,m4),“kode_faktur”2有4个item-id(m2,l1,f2,d1)
在我的ss下面我已经成功创建了一个2d数组(872行和20列),其中20列是我从每个事务的最大项目ID数中获得的数字可以有
在这种情况下,我正在使用下一个循环,在这种情况下,我使用两个进行下一次循环,我已成功循环第一个事务,但问题是 = for looping 的外部无法正常工作。外部for循环无限..
对于更具体的我已经包括下面的ss将解释一切
我的要求就像这样
使用2d数组将两个字段转换为稀疏矩阵
将2d数组绑定到datagrid
我已经解决了第二项任务。为什么我这样做?因为我想将这个稀疏矩阵数组与另一个1d数组进行比较,从中创建一个新数组,并用网格显示它。
感谢您的关注
的问候,我
这是完整的代码
'FILL X
Dim nil_x As String = "SELECT max( kode_faktur ) FROM detail"
Dim x As Int32
Dim CMD_X = New MySqlCommand(nil_x, conn.konek)
x = Convert.ToInt32(CMD_X.ExecuteScalar())
'FILL Y
Dim nil_y As String = "select max(x.jumlah) from (select count(*) as jumlah from detail group by kode_faktur)x"
Dim y As Int32
CMD_Y = New MySqlCommand(nil_y, conn.konek)
y = Convert.ToInt32(CMD_Y.ExecuteScalar())
'LOOPING ARRAY TRANS
Dim i, j As Integer
Dim arayT(,) As String
Dim hasil As MySqlDataReader
ReDim arayT(x, y)
For i = 0 To x
Dim msql As String = "select kode_brg from detail where kode_faktur = '" & i & "' "
CMD = New MySqlCommand(msql, conn.konek)
hasil = CMD.ExecuteReader()
Dim row_y As String = "select count(kode_brg) from detail where kode_faktur = '" & i & "' "
Dim y2 As Int32
CMD_Y2 = New MySqlCommand(row_y, conn.konek)
y2 = Convert.ToInt32(CMD_Y2.ExecuteScalar())
If hasil.HasRows Then
For j = 0 To y2 - 1
hasil.Read()
arayT(i, j) = hasil("kode_brg")
Next j
Else
Continue For
Exit For
End If
'Exit For
Next i
conn.konek.Close()
这是ss
答案 0 :(得分:1)
要手动创建Sparse matrix,您可以这样做:
Dim matrix As Integer()() = New Integer(4)() {}
matrix(0) = New Integer() {1, 2, 0, 0, 0, 0, 0}
matrix(1) = New Integer() {0, 3, 4, 0, 0, 0, 0}
matrix(2) = New Integer() {0, 0, 5, 6, 7, 0, 0}
matrix(3) = New Integer() {0, 0, 0, 0, 0, 8, 0}
matrix(4) = New Integer() {0, 0, 0, 0, 0, 0, 9}
以编程方式创建一个:
Dim ROW_COUNT As Integer = 5
Dim COLUMN_COUNT As Integer = 7
Dim matrix As Integer()() = New Integer(ROW_COUNT - 1)() {}
For rowIndex As Integer = 0 To (ROW_COUNT - 1)
matrix(rowIndex) = New Integer(COLUMN_COUNT - 1) {}
For columnIndex As Integer = 0 To (COLUMN_COUNT - 1)
matrix(rowIndex)(columnIndex) = (10 Mod ((rowIndex + 1) + (columnIndex + 1)))
Next
Next
结果:
0, 1, 2, 0, 4, 3, 2
1, 2, 0, 4, 3, 2, 1
2, 0, 4, 3, 2, 1, 0
0, 4, 3, 2, 1, 0, 10
4, 3, 2, 1, 0, 10, 10
如果要测试两个矩阵是否相等,可以创建如下函数:
Public Shared Function MatrixEquals(m1 As Integer()(), m2 As Integer()()) As Boolean
If (m1 Is Nothing) Then
Return (m2 Is Nothing)
End If
If (m2 Is Nothing) Then
Return False
End If
If (m1.Length <> m2.Length) Then
Return False
End If
For i As Integer = 0 To (m1.Length - 1)
If (Not m1(i).SequenceEqual(m2(i))) Then
Return False
End If
Next
Return True
End Function