查询在MySQL中执行,但调试在vb.net中出错

时间:2016-05-23 14:00:47

标签: mysql vb.net

我有一个插入查询,我试图将table1数据复制到table2。现在,当我直接在MySQL中执行但是当我尝试通过VB.Net“

进行调试时,查询工作正常
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

1 个答案:

答案 0 :(得分:0)

我相信您的错误来自于您使用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

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