0

DB のランダム レコードから 2D 文字列配列/行列を返す (はずの) 関数があります。

Function GetRndRecords(table As String, fields As String(), num_records As Integer) As String(,)
    If not connected Then
        Connect()
    End If
    Dim Rand as New Random()
    Dim strSQL As String
    Dim ID_Max As Integer
    Dim Results(num_records,fields.Length) As String
    strSQL = "SELECT COUNT(*) FROM " & table
    dbcomm = New OleDbCommand(strSQL,dbconn)
    ID_Max = dbcomm.ExecuteScalar()
    Dim i As Integer
    For i=0 To num_records-1
        Do
            Dim Rand_ID As Integer = Rand.Next(1,ID_Max)
            Dim j As Integer
            strSQL = "SELECT "                                      ' strSQL begin
            For j = 0 To fields.Length-1
                If(j <> 0) Then
                    strSQL &= ", "
                End If
                strSQL &= fields(j)
            Next
            strSQL &= " FROM " & table & " WHERE ID = " & Rand_ID   ' strSQL end
            dbcomm = New OleDbCommand(strSQL,dbconn)
            dbread = dbcomm.ExecuteReader()
            dbread.Read()
            For j = 0 To fields.Length-1
                Results(i,j) = dbread.GetString(j)
            Next
        Loop Until dbread.GetString(0) <> ""
    Next
    Return Results
End Function

この関数で問題は発生しませんが、(Page_Load イベントで) 呼び出すと ("BC30201: Expression expected.") が発生します。

Sub Page_Load
    Dim Rand_Records As String(,)
    Rand_Records = GetRndRecords("Autore",{"ID","Nome"},PossibleAnswers-1)
End Sub

私もこれを試しましたが、同じ結果でした

Sub Page_Load
    Dim Rand_Records As String(,)
    Rand_Records = new String({"ID1","Nome1"},{"ID2","Nome2"},{"ID3","Nome3"})
End Sub

誰かが私に理由を説明できますか?

4

0 に答える 0