MySQL 数据读取器未进入循环 [英] MySQL Data Reader not entering the loop

查看:60
本文介绍了MySQL 数据读取器未进入循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了很多不同的方法,但此时我被难住了.

I have tried many different things but at this point I am stumped.

Public Function GetCust(email As String) As Integer
    Dim cs As String = <connection string>
    Dim conn As New MySqlConnection(cs)
    Dim custId As Integer
    Dim cust_query As String = "SELECT entity_id FROM customer_entity WHERE email = '@email'"
    Dim com As New MySqlCommand(cust_query, conn)
    Dim reader As MySqlDataReader

    Try
        conn.Open()
        com.Parameters.Add("@email", MySqlDbType.String)
        com.Parameters("@email").Value = email
        reader = com.ExecuteReader
        While reader.Read()
            custId = reader.GetInt16(0)
        End While
        MsgBox(custId)
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

    Return custId
End Function

它应该只从表中提取一列和一行,所以我应该只从查询中提取一个值,但它没有进入循环.似乎它是我的语法错误,但似乎无法找到它.提前致谢!

It should be pulling only one column and one row from a table so i should only have one value out of the query but it isn't entering the loop. Seems like its a syntax error on my part but can't seem to find it. Thanks in advance!

推荐答案

您应该删除 SQL 字符串中电子邮件参数周围的引号:

You should remove the quotes around the email parameter in the SQL string:

Dim cust_query As String = "SELECT entity_id FROM customer_entity WHERE email = @email"

这将生成看起来像

WHERE email = ''myemail@mydomain.com''

什么时候该

WHERE email = 'myemail@mydomain.com'

这篇关于MySQL 数据读取器未进入循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆