VB.NET 使用数据库字段填充二维数组时出错 [英] VB.NET Error Populating 2d Array with database field

查看:23
本文介绍了VB.NET 使用数据库字段填充二维数组时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含字段kode_brg"的查询我想将它们填充到 array2d 中,我正在使用 mysqldatareader 将字段填充到 array2d 中,但似乎 datareader 只在 while 循环中读取一次更具体地说,我添加了 myproblem belom 的 ss

i have a query that contains field "kode_brg" i want to fill them into array2d, i'm using mysqldatareader to fill the field into array2d, but it seems that the datareader only read once inside the while looping for more specific, i've added the ss of myproblem belom

我很确定我在while循环

有人能帮我解决这个问题吗之前谢谢问候,我

could somebody help me fix this problem thanks before regards, me

已为问题添加了代码

    '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 msql As String = "select kode_brg from detail group by kode_faktur"
    Dim i, j As Integer
    Dim arayT(,) As String

    CMD = New MySqlCommand(msql, conn.konek)

    'Try
    Dim hasil As MySqlDataReader
    hasil = CMD.ExecuteReader()

    While hasil.Read()

        For i = 0 To x
            ReDim Preserve arayT(x, y)
            For j = 0 To y

                arayT(i, j) = hasil("kode_brg")
                'Continue For
            Next j
            j += 1
            'Continue For
        Next i
        i += 1
        Exit While
    End While

    conn.konek.Close()

    dgv2.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing
    dgv2.RowHeadersWidthSizeMode =    DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders
    dgv2.RowHeadersVisible = False

    With Me.dgv2
        .DataSource = New Mommo.Data.ArrayDataView(arayT)
        .SelectionMode = DataGridViewSelectionMode.FullRowSelect
        .AllowUserToAddRows = False
        .RowHeadersVisible = True
    End With

我的一个重要通知不要建议我使用 ARRAYLIST/LIST OF/LINQ/DATASET/DATATABLE因为我真正想做的是用数据库字段填充二维数组,将它与另一个一维数组进行比较,然后从它们两个创建一个新的二维数组,最后我想将新的二维数组显示到网格中..这就是为什么我选择了二维数组而不是使用数据集来填充我的网格

one big note from me DON'T SUGGEST ME TO USE ARRAYLIST/LIST OF/LINQ/DATASET/DATATABLE because what i really want to do is filling the 2d array with database field, comparing it with another 1d array, then create a new 2d array from both of them, finally i want to display the newly 2d array into the grid.. that's why i chose 2d array instead using dataset to fill my grid

我不知道如何将 list(of) 或 arraylist 与一维数组进行比较,这就是我使用二维数组的原因(基本)之前谢谢你:)

i don't know how to compare list(of) or arraylist with 1d array, that's why i'm using 2d array (basic) thanks you before :)

想要的输出

推荐答案

假设您设法使数组大小与查询返回的数据数量完全相同,您可以尝试将循环更改为以下内容:

Assuming you managed to get array size exactly the same as number of data returned from query, you can try to change the loop to following :

ReDim Preserve arayT(x, y)
For i = 0 To (x-1)
    For j = 0 To (y-1)
        hasil.Read()
        arayT(i, j) = hasil("kode_brg")
        'Continue For
    Next j
    'Continue For
Next i

这篇关于VB.NET 使用数据库字段填充二维数组时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆