ASP.NET SQL查询未返回预期结果

时间:2015-03-02 09:04:39

标签: mysql asp.net sql-server vb.net iis

获取以下代码的问题。我下面基本上创建的是,当用户登录到我的网站时,他们将点击此aspx页面,这将检查您是否属于group_id 1001。从数据库的角度来看,我确保我的测试登录用户名在group_ID 1001的数据库中,但是我认为我没有group_id。我想也许我的SQL查询尝试了@username'" & username & "',但仍然返回false。没有看到任何其他问题,但希望看到有人抓到我的错误。我的钱是在SQL查询的某个地方,但我做的没有解决问题。

<%@ import namespace="System.Data" %> 
<%@ import namespace="MySql.data.MySqlclient" %> 
<%@ import namespace="MySql.data.MySqlclient.MySqlConnection" %> 

    <Script runat="server"> 
        Sub page_load()
            Dim username As String = Convert.ToString(User.Identity.Name.Substring(User.Identity.Name.IndexOf("\") + 1))
            Dim dbconn As MySqlConnection
            Dim mySqlCommand As MySqlCommand
            Dim counter As Integer
            Dim isInGroup As Boolean
            Dim strSQL As String = "SELECT COUNT(*) FROM tbl_staff WHERE username = @username AND group_id = 1001;"

            dbconn = New MySqlConnection("server=localhost;database=imc_directory_tool;user=Directory_Admin;port=3306;password=IMCisgreat2014;")
            dbconn.Open()

            mySqlCommand = New MySqlCommand(strSQL, dbconn)
            mySqlCommand.Parameters.AddWithValue("@username", username)
            counter = mySqlCommand.ExecuteScalar()
            If isInGroup = counter > 0 Then
                Response.Write("You are part of group_id 1001")
            Else
                Response.Write("You are not part of group_id 1001")
            End If
            dbconn.Close()
        End Sub
    </Script>  

1 个答案:

答案 0 :(得分:0)

布尔值的默认值为false。所以在你宣布之后:

Dim isInGroup As Boolean

isInGroup = False

这意味着你的代码

If isInGroup = counter > 0 Then
    Response.Write("You are part of group_id 1001")
Else
    Response.Write("You are not part of group_id 1001")
End If

实际上是:

If False = (counter > 0) Then
    Response.Write("You are part of group_id 1001")
Else
    Response.Write("You are not part of group_id 1001")
End If

在数据库中找到您的用户名和组合

(counter > 0) = True

我不知道你为什么要使用isInGroup布尔值,但是如果你需要将其用于其他代码,那么我建议你先设置布尔值然后在需要的地方进行测试。

isInGroup = counter > 0

If isInGroup = True Then
    Response.Write("You are part of group_id 1001")
Else
    Response.Write("You are not part of group_id 1001")
End If