ODBC select语句得到一个布尔 [英] ODBC select statement to get a boolean

查看:162
本文介绍了ODBC select语句得到一个布尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查用户名和密码是否存在于我的MySQL数据库,如果是这样,我需要返回true,否则为false。这是我有个大气压:

  myconn.Open()

昏暗selectSQL作为字符串=SELECT *
                             会员
                            其中username =&放大器; objMember.Username&安培;
                              和密码=&放大器; objMember.Password
昏暗CMD作为新的OdbcCommand(selectSQL,的myconn)

cmd.ExecuteNonQuery()

如果cmd.Parameters.Count = 1,则
    返回TRUE
其他
    返回False
结束如果

myconn.Close()
myconn.Dispose()
 

我得到的是0,即使用户名和密码存在!也许我错了,我的编码?


解决方案

  myconn.Open()

昏暗的计数为整数= 0

昏暗selectSQL作为字符串=SELECT COUNT(*)
                             会员
                            其中username =?
                              和密码=?
昏暗CMD作为新的OdbcCommand(selectSQL,的myconn)

cmd.Parameters.AddWithValue(LidLoginnaam,objLid.LidLoginnaam)
cmd.Parameters.AddWithValue(LidWachtwoord,objLid.LidWachtwoord)

数= Convert.ToInt32(cmd.ExecuteScalar())

如果计数= 1,则
    返回TRUE
其他
    返回False
结束如果

myconn.Close()
myconn.Dispose()
 

解决方案

不要使用字符串连接来建立你的SQL查询,使用参数来代替。

http://msdn.microsoft.com/ EN-US /库/ system.data.odbc.odbcparameter.aspx

 昏暗的计数为整数= 0

尝试
    昏暗的SQL作为字符串=SELECT COUNT(*)FROM成员其中username = @用户名和密码= @密码
    昏暗CMD作为新的SqlCommand(SQL,康涅狄格州)
    cmd.Parameters.AddWithValue(@用户名,objMember.Username)
    cmd.Parameters.AddWithValue(@密码,objMember.Password)
    数= Convert.ToInt32(cmd.ExecuteScalar())
抓住EX为例外
    Console.WriteLine(ex.Message)
结束尝试

返回(计数大于0)
 

如果您不使用从您的查询检索的数据,然后只使用的ExecuteScalar ,以获得符合您的用户名和密码的记录数。

http://msdn.microsoft.com/en-us/library/system.data.odbc.odbccommand.executescalar.aspx

这基本上返回TRUE,如果计数> 0(意思是有匹配的用户名和密码的记录)。

还检查了不同的命令执行方法之间的区别在这里:的http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand(v=vs.71).aspx.您正在使用的的ExecuteNonQuery 作为检索记录这是不正确的这一目的。

希望这有助于。

I'm trying to check whether a Username and Password exist in my MySQL database and if so I need to return true, otherwise false. This is what I have atm:

myconn.Open()

Dim selectSQL As String = "SELECT * 
                             FROM member 
                            WHERE Username = " & objMember.Username & " 
                              AND Password= " & objMember.Password
Dim cmd As New OdbcCommand(selectSQL, myconn)

cmd.ExecuteNonQuery()

If cmd.Parameters.Count = 1 Then
    Return True
Else
    Return False
End If

myconn.Close()
myconn.Dispose()

All I get is 0, even though the Username and Password exist! Or perhaps I'm wrong with my coding?


SOLUTION

myconn.Open()

Dim count As Integer = 0

Dim selectSQL As String = "SELECT COUNT(*)
                             FROM member 
                            WHERE Username = ? 
                              AND Password= ?"
Dim cmd As New OdbcCommand(selectSQL, myconn)

cmd.Parameters.AddWithValue("LidLoginnaam", objLid.LidLoginnaam)
cmd.Parameters.AddWithValue("LidWachtwoord", objLid.LidWachtwoord)

count = Convert.ToInt32(cmd.ExecuteScalar())

If count = 1 Then
    Return True
Else
    Return False
End If

myconn.Close()
myconn.Dispose()

解决方案

Do not use string concatenation to build your SQL queries, use parameters instead.

http://msdn.microsoft.com/en-us/library/system.data.odbc.odbcparameter.aspx

Dim count as Integer = 0

Try
    Dim sql As String = "SELECT COUNT(*) FROM member WHERE Username = @username AND Password = @password"
    Dim cmd As New SqlCommand(sql, conn)
    cmd.Parameters.AddWithValue("@username", objMember.Username)
    cmd.Parameters.AddWithValue("@password", objMember.Password)
    count = Convert.ToInt32(cmd.ExecuteScalar())
Catch ex As Exception
    Console.WriteLine(ex.Message)
End Try

Return (count > 0)

If you don't use the data retrieved from your query, then just use ExecuteScalar to get the number of records that matched your Username and Password.

http://msdn.microsoft.com/en-us/library/system.data.odbc.odbccommand.executescalar.aspx

This basically returns TRUE if count > 0 (meaning there is a record that matched the Username and Password).

Also check out the distinction between the different command execution methods here: http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand(v=vs.71).aspx. You are using ExecuteNonQuery for retrieving records which is incorrect for this purpose.

Hope this helps.

这篇关于ODBC select语句得到一个布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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