【问题标题】:Query getting executed in MySQL but while debugging gets error in vb.net查询在 MySQL 中执行,但在 vb.net 中调试时出错
【发布时间】:2016-05-23 14:00:47
【问题描述】:

我有一个插入查询,我试图将 table1 数据复制到 table2。现在,当我直接在 MySQL 中执行但尝试通过 VB.Net 进行调试时,Query 工作正常"

INSERT INTO newMedicinesOrders (`OrderID`,`medicineName`, `power`, `form`, `fQuantity`, `iQuantity`, `type`, `cost`, `prescriptionLink`, `userID`) SELECT `orderID`, `name`, `power`, `form`, `fQuantity`, `iQuantity`, `type`, `mrp`, `prescriptionLink`, `userID` from myCart WHERE userID = '1'

我收到一条错误消息

“字段列表”中的未知列“orderID”

vb代码

Try
            Dim str1 As String = "INSERT INTO newMedicinesOrders (`OrderID`,`medicineName`, `power`, `form`, `fQuantity`, `iQuantity`, `type`, `cost`, `prescriptionLink`, `userID`) SELECT `orderID`, `name`, `power`, `form`, `fQuantity`, `iQuantity`, `type`, `mrp`, `prescriptionLink`, `userID` from myCart WHERE userID = '" + userid.Text + "'"

            Dim str2 As MySqlDataReader
            Dim adapter As New MySqlDataAdapter
            Dim command As New MySqlCommand
            command.CommandText = str1
            command.Connection = con
            adapter.SelectCommand = command
            con.Open()
            str2 = command.ExecuteReader
            con.Close()
            Response.Write("<script language='javascript'>alert('Success.');</script>")
        Catch ex As Exception
            Response.Write(ex)
        End Try

【问题讨论】:

  • @eggyal 它将扩展消息显示为“'字段列表'中的未知列'orderID'”
  • 我们能看到在 vb.net 中调用这个查询的代码吗?
  • @eggyal 我给你看调试模式截图可以吗?
  • 最好将代码复制/粘贴到您的问题中,但如果有信息只能在屏幕截图中传达,那么请务必这样做。
  • 不确定这是否会有所不同,但是您能否将值 1 硬编码到 .net 中以查看它是否可行,因为这是唯一的区别?

标签: mysql vb.net


【解决方案1】:

我相信你的错误来自于你使用SQLCommand.ExecuteReader()

来自this description

ExecuteReader 用于将查询结果作为 DataReader 对象获取。它是只读的,只向前检索记录,它使用 select 命令从第一个到最后一个读取表。

ExecuteNonQuery 用于执行不返回任何数据的查询。用于执行更新、插入、删除等sql语句。ExecuteNonQuery执行命令并返回受影响的行数。

根据MSDN,这是您执行INSERTUPDATEDELETE 语句的方式:

Public Sub CreateCommand(ByVal queryString As String, ByVal connectionString As String)
    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)
        command.Connection.Open()
        command.ExecuteNonQuery()
    End Using
End Sub

但是,我没有找到有关使用ExecuteReader() 执行INSERT 命令时会发生什么的信息,但我想这就是发生的情况...

【讨论】:

    猜你喜欢
    • 2012-11-04
    • 2017-03-03
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 2018-06-07
    • 2012-11-18
    相关资源
    最近更新 更多