首页 > 解决方案 > Visual basic 上的多个查询

问题描述

如何使用带参数的多个查询?我在不使用参数的情况下尝试了这个,它正在工作,请在我的第二个查询中帮助我...请帮助..

编辑:我的“query2”不工作,每次我添加或插入时,我的数据库表(history_pay)中没有数据纯空白......

con.Open()

    Try
        cmd = con.CreateCommand()
        cmd.CommandText = "INSERT INTO studentpay(id,student_name,course,year_level,semester,units,amount,orno,ordate,cashier,paymentfor,tuition,amountpay,balance) VALUES (@id,@student_name,@course,@year_level,@semester,@units,@amount,@orno,@ordate,@cashier,@paymentfor,@tuition,@amountpay,@balance)"


        ' this is my second query no inserting data
        query2 = "INSERT INTO history_pay (id,student_name,last_date,last_amountpaid,last_balance) VALUES (@id,@student_name,@ordate,@amount,@balance)"
        Dim QueryString As String = String.Concat(cmd, ";", query2)
        Dim command As New MySqlCommand(QueryString, con)


        cmd.Parameters.AddWithValue("@id", txtstudentid.Text)
        cmd.Parameters.AddWithValue("@student_name", txtstudentname.Text)
        cmd.Parameters.AddWithValue("@course", cblcourse.Text)
        cmd.Parameters.AddWithValue("@year_level", cblyear.Text)
        cmd.Parameters.AddWithValue("@semester", cblsemester.Text)
        cmd.Parameters.AddWithValue("@units", txtunits.Text)
        cmd.Parameters.AddWithValue("@amount", txtamountpay.Text)
        cmd.Parameters.AddWithValue("@orno", txtorno.Text)
        cmd.Parameters.AddWithValue("@ordate", txtordate.Text)
        cmd.Parameters.AddWithValue("@cashier", txtcashier.Text)
        cmd.Parameters.AddWithValue("@paymentfor", cblpaymentfor.Text)
        cmd.Parameters.AddWithValue("@tuition", lbltuition.Text)
        cmd.Parameters.AddWithValue("@amountpay", lblamountspay.Text)
        cmd.Parameters.AddWithValue("@balance", lblbalance.Text)
        cmd.ExecuteNonQuery()
        MsgBox("Sucessfully", MessageBoxButtons.OK, MessageBoxIcon.Information)
        load_data()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

标签: vb.net

解决方案


首先,你的表设计看起来飘忽不定。我在两个不同的表中看到重复值。您应该查看参考设计。

其次,我建议研究对象参考模型 (ORM),因为它可以为您节省大量时间,同时还可以为您提供编译时间检查。我认为它对于具有 50,000 多行代码的大型项目非常宝贵。你需要学习 LINQ,但我还是建议学习 LINQ,因为它是一个强大的数据挖掘和操作工具。

我建议使用 Command.Parameters.Add 而不是 Command.Parameters.AddWithValue,但这更多是个人喜好,因为我使用 TSQL,并且我喜欢对输入进行精细控制。

我推荐您使用参数而不是字符串连接。这有助于避免注入攻击。

至于您的问题,鉴于您提供的信息,如果我不能使用 ORM 并且我不能使表格设计更有效,我会这样做。

Using MySQLSERVER_Connection As New MySql.Data.MySqlClient.MySqlConnection("<Your Login Details>")
    Using MySQLCommand As New MySql.Data.MySqlClient.MySqlCommand()
        MySQLCommand.Connection = MySQLSERVER_Connection
        Dim Command As New Text.StringBuilder
        Command.AppendLine("INSERT INTO studentpay")
        Command.AppendLine("(id,student_name,course,year_level,semester,units,amount,orno,ordate,cashier,paymentfor,tuition,amountpay,balance)")
        Command.AppendLine("VALUES")
        Command.AppendLine("(@id,@student_name,@course,@year_level,@semester,@units,@amount,@orno,@ordate,@cashier,@paymentfor,@tuition,@amountpay,@balance);")
        MySQLCommand.Parameters.AddWithValue("@id", txtstudentid.Text)
        MySQLCommand.Parameters.AddWithValue("@student_name", txtstudentname.Text)
        MySQLCommand.Parameters.AddWithValue("@course", cblcourse.Text)
        MySQLCommand.Parameters.AddWithValue("@year_level", cblyear.Text)
        MySQLCommand.Parameters.AddWithValue("@semester", cblsemester.Text)
        MySQLCommand.Parameters.AddWithValue("@units", txtunits.Text)
        MySQLCommand.Parameters.AddWithValue("@amount", txtamountpay.Text)
        MySQLCommand.Parameters.AddWithValue("@orno", txtorno.Text)
        MySQLCommand.Parameters.AddWithValue("@ordate", txtordate.Text)
        MySQLCommand.Parameters.AddWithValue("@cashier", txtcashier.Text)
        MySQLCommand.Parameters.AddWithValue("@paymentfor", cblpaymentfor.Text)
        MySQLCommand.Parameters.AddWithValue("@tuition", lbltuition.Text)
        MySQLCommand.Parameters.AddWithValue("@amountpay", lblamountspay.Text)
        MySQLCommand.Parameters.AddWithValue("@balance", lblbalance.Text)
        MySQLSERVER_Connection.Open()
        MySQLCommand.ExecuteNonQuery()
        MySQLSERVER_Connection.Close()
    End Using
    Using MySQLCommand As New MySql.Data.MySqlClient.MySqlCommand()
        MySQLCommand.Connection = MySQLSERVER_Connection
        Dim Command As New Text.StringBuilder
        Command.AppendLine("INSERT INTO history_pay")
        Command.AppendLine("(id,student_name,last_date,last_amountpaid,last_balance)")
        Command.AppendLine("VALUES")
        Command.AppendLine("(@id,@student_name,@ordate,@amount,@balance);")
        MySQLCommand.Parameters.AddWithValue("@id", txtstudentid.Text)
        MySQLCommand.Parameters.AddWithValue("@student_name", txtstudentname.Text)
        MySQLCommand.Parameters.AddWithValue("@amount", txtamountpay.Text)
        MySQLCommand.Parameters.AddWithValue("@ordate", txtordate.Text)
        MySQLCommand.Parameters.AddWithValue("@amountpay", lblamountspay.Text)
        MySQLCommand.Parameters.AddWithValue("@balance", lblbalance.Text)
        MySQLSERVER_Connection.Open()
        MySQLCommand.ExecuteNonQuery()
        MySQLSERVER_Connection.Close()
    End Using
End Using

我正在使用一个连接和两个命令。请注意,我无法测试此代码。


推荐阅读