如何在vb.net中用for子句从数据库mysql中获取数据

bbmckpt7  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(380)

我在vb.net上有一个代码,我想用vb.net的循环从mysql数据库中检索数据。
我面临的问题似乎是重复循环工作不正常,获得的数据只重复第一个数据。。
有人能帮我吗??

conn.Open()
sqlConn1 = "select * from transaksi"
data3 = New MySqlDataAdapter(sqlConn1, conn)
data3.Fill(ds3, "transaksi")

Dim keterangan As String

Dim c4 As String = String.Empty
For c As Integer = 0 To ds3.Tables("transaksi").Rows.Count - 1

    sqlConn2 = "select * from transaksi"
    data32 = New MySqlDataAdapter(sqlConn2, conn)
    data32.Fill(ds32, "transaksi")

    c4 = ds32.Tables("transaksi").Rows(c).Item("idTransaksi")

    'For d As Integer = 0 To s - 1

    Dim nilaihasil As Double
    Dim nilaix As Double
    Dim var_a As Double
    Dim var_b As Double

    For f As Integer = 0 To 5 - 1
        sqlConn6 = "select * from transaksidetail WHERE idTransaksi = '" & c4 & "'"
        data6 = New MySqlDataAdapter(sqlConn6, conn)
        data6.Fill(ds6, "transaksidetail")

        var_a = ds6.Tables("transaksidetail").Rows(f).Item("nilaiTransaksidetail")

        sqlConn7 = "select nilai from cluster WHERE idKriteria = '" & ds6.Tables("transaksidetail").Rows(f).Item("idKriteria") & "' AND kode = 'C0'"
        data7 = New MySqlDataAdapter(sqlConn7, conn)
        data7.Fill(ds7, "cluster")
        var_b = ds7.Tables("cluster").Rows(f).Item("nilai")

        'MessageBox.Show(var_a & var_b)
        nilaix += ((var_a ^ 2) - (var_b ^ 2))

    Next

    nilaihasil = Math.Sqrt(nilaix)

    perintah.CommandType = CommandType.Text
    perintah.CommandText = "INSERT INTO tempcluster (idTemp,idTransaksi,nilai,cluster) VALUES ('','" & c4 & "','" & nilaihasil & "','C0')"
    perintah.Connection = conn
    perintah.ExecuteNonQuery()

Next c
conn.Close()
ajsxfq5m

ajsxfq5m1#

我认为您的问题可能是在循环中重新填充数据表transaksi。注解和解释一致。

Private Sub OPCode2()
    Dim conn As New MySqlConnection("Your Connection String")
    conn.Open()
    Dim sqlConn1 = "select * from transaksi"
    Dim data3 = New MySqlDataAdapter(sqlConn1, conn)
    Dim ds3 As New DataSet
    data3.Fill(ds3, "transaksi")

    Dim keterangan As String = ""

    Dim c4 As String = String.Empty
    For c As Integer = 0 To ds3.Tables("transaksi").Rows.Count - 1
        'It looks like you are doing the same thing twice
        'Anyway, you don't want to do it over and over in the loop
        'sqlConn2 = "select * from transaksi"
        'data32 = New MySqlDataAdapter(sqlConn2, conn)
        'data32.Fill(ds32, "transaksi")

        c4 = ds3.Tables("transaksi").Rows(c).Item("idTransaksi").ToString

        Dim nilaihasil As Double
        Dim nilaix As Double
        Dim var_a As Double
        Dim var_b As Double

        Dim sqlConn6 = "select * from transaksidetail WHERE idTransaksi = '" & c4 & "'"
        Dim data6 = New MySqlDataAdapter(sqlConn6, conn)
        Dim ds6 As New DataSet
        data6.Fill(ds6, "transaksidetail")

        'This looks a little silly, how about To 4
        For f As Integer = 0 To 5 - 1
            'Move the following out of the loop
            'No sense doing the same thing 5 times
            'sqlConn6 = "select * from transaksidetail WHERE idTransaksi = '" & c4 & "'"
            'data6 = New MySqlDataAdapter(sqlConn6, conn)
            'data6.Fill(ds6, "transaksidetail")

            var_a = CDbl(ds6.Tables("transaksidetail").Rows(f).Item("nilaiTransaksidetail"))

            Dim sqlConn7 = "select nilai from cluster WHERE idKriteria = '" & ds6.Tables("transaksidetail").Rows(f).Item("idKriteria").ToString & "' AND kode = 'C0'"
            Dim data7 = New MySqlDataAdapter(sqlConn7, conn)
            Dim ds7 As New DataSet
            data7.Fill(ds7, "cluster")
            var_b = CDbl(ds7.Tables("cluster").Rows(f).Item("nilai"))

            'MessageBox.Show(var_a & var_b)
            nilaix += ((var_a ^ 2) - (var_b ^ 2))

        Next

        nilaihasil = Math.Sqrt(nilaix)
        Dim perintah As New MySqlCommand
        perintah.CommandType = CommandType.Text
        perintah.CommandText = "INSERT INTO tempcluster (idTemp,idTransaksi,nilai,cluster) VALUES ('','" & c4 & "','" & nilaihasil & "','C0')"
        perintah.Connection = conn
        perintah.ExecuteNonQuery()

    Next c
    conn.Close()
End Sub

相关问题