Vb.net 基于visualbasic的多查询

Vb.net 基于visualbasic的多查询,vb.net,Vb.net,如何使用带参数的多个查询? 我在没有使用参数的情况下尝试了这一点,它正在工作 请帮我回答第二个问题。。。 请帮忙 编辑:我的“query2”不起作用,每次我添加或插入时,我的数据库表(history_pay)中没有任何数据纯空 con.Open() 首先,你的桌子设计看起来不稳定。我在两个不同的表中看到重复的值。你应该研究一下参考设计 其次,我建议研究对象引用模型(ORM),因为它可以节省大量时间,同时还可以进行编译时检查。我认为对于具有50000行代码的大型项目来说,它是非常宝贵的。您需要学习

如何使用带参数的多个查询? 我在没有使用参数的情况下尝试了这一点,它正在工作 请帮我回答第二个问题。。。 请帮忙

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

con.Open()


首先,你的桌子设计看起来不稳定。我在两个不同的表中看到重复的值。你应该研究一下参考设计

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

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

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

至于你的问题,根据你提供的信息,如果我不能使用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
使用MySQLSERVER\u连接作为新的MySql.Data.MySqlClient.MySqlConnection(“”)
将MySQLCommand用作新的MySql.Data.MySqlClient.MySQLCommand()
MySQLCommand.Connection=MySQLSERVER\u连接
Dim命令作为新的Text.StringBuilder
命令.AppendLine(“插入studentpay”)
命令.AppendLine((id、学生姓名、课程、年级、学期、单位、金额、orno、订单日期、出纳、付款、学费、金额、余额)
命令行(“值”)
命令.AppendLine((@id、@student\u name、@course、@year\u level、@sement、@units、@amount、@orno、@ordate、@cashier、@paymentfor、@tutory、@amountpay、@balance);)
MySQLCommand.Parameters.AddWithValue(“@id”,txtstudentid.Text)
MySQLCommand.Parameters.AddWithValue(“@student\u name”,txtstudentname.Text)
MySQLCommand.Parameters.AddWithValue(“@course”,cblcourse.Text)
MySQLCommand.Parameters.AddWithValue(“@year\u level”,cblyear.Text)
MySQLCommand.Parameters.AddWithValue(“@sement”,cblsemester.Text)
MySQLCommand.Parameters.AddWithValue(“@units”,txtnits.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(“@tutory”,lbltuition.Text)
MySQLCommand.Parameters.AddWithValue(“@amountpay”,lblamountspay.Text)
MySQLCommand.Parameters.AddWithValue(“@balance”,lblbalance.Text)
MySQLSERVER_Connection.Open()
MySQLCommand.ExecuteNonQuery()
MySQLSERVER_Connection.Close()
终端使用
将MySQLCommand用作新的MySql.Data.MySqlClient.MySQLCommand()
MySQLCommand.Connection=MySQLSERVER\u连接
Dim命令作为新的Text.StringBuilder
命令.AppendLine(“插入到历史记录中”)
命令.AppendLine(((id,学生姓名,最后日期,最后支付金额,最后余额))
命令行(“值”)
命令行(@id,@student_name,@ordate,@amount,@balance);)
MySQLCommand.Parameters.AddWithValue(“@id”,txtstudentid.Text)
MySQLCommand.Parameters.AddWithValue(“@student\u 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()
终端使用
终端使用

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

请尝试提供问题的完整而清晰的解释。您的代码根本没有意义。拿起笔和纸,一步一步地写下你需要做的事情,然后写下代码。您以一种毫无意义的方式创建
QueryString
,然后甚至不使用
命令。如果要同时执行这两条语句,为什么首先要有两个命令对象?你有没有看过
QueryString
中包含的内容?因为你从来没有执行过
命令
我不知道你希望它如何插入数据。这是一个不同的表,有不同的参数。将其作为第二个查询运行。阅读并学习如何有效地使用网站不要让你的连接像那样打开。将所有一次性物品放在使用垫块中。只需运行两个单独的查询。“MySQL库试图通过不允许运行第二个命令来阻止注入攻击”我在
SELECT LAST_INSERT_ID()的分号后运行第二个命令并运行。也许这是版本特定的?我上一次提出这个问题是大约十年前。当时我只是决定使用多个执行语句。我还记得,我可以在同一个命令中执行一次,然后选择一次,以获取最后插入的ID号。自从发现ORMs以来,在过去的三年里,我并没有在.NET程序中使用过directsql。对于任何非常复杂的事情,我都倾向于使用数据库函数和过程。在语句中多次使用参数可能会造成问题。尝试使用相同的参数进行双重插入。已尝试。很好。删除了关于双重指令歼灭的虚假陈述。
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