我尝试了很多不同的东西,但此时我很难过。
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
它应该只从表中拉出一列和一行,所以我只应该从查询中获得一个值,但它不会进入循环。看起来像我的语法错误,但似乎无法找到它。提前谢谢!
答案 0 :(得分:1)
您应该删除SQL字符串中的email参数周围的引号:
Dim cust_query As String = "SELECT entity_id FROM customer_entity WHERE email = @email"
这将生成类似于
的SQLWHERE email = ''myemail@mydomain.com''
什么时候应该
WHERE email = 'myemail@mydomain.com'