简体   繁体   English

如何查看要从VB.net代码传递到SQL的查询

[英]How to see what query is being passed to SQL from VB.net code

I have a GetUSerID function that runs a SQL query to get the UserID.The query works and returns a userID, but I don't think it is applying the WHERE condition. 我有一个GetUSerID函数,该函数运行SQL查询以获取UserID。查询有效并返回一个UserID,但我认为它没有应用WHERE条件。 IT simply returns the highest UserID in the Users table. IT只是在“用户”表中返回最高的UserID。 (I tested this by adding a new user (thus the highest userID changed) then logging in again- and the displayed userId increased top the NOW highest userID. I believe it is just doing "seclect userID from users" and grapping the first result and the "WHERE username=" is not being applied. I know the process I am using for getting the username works- as when I declare that as a varibale and just display it straight away- the username is correct- but it doesn't display the useRID that goes along with that username. (我通过添加新用户(更改了最高用户ID)然后再次登录进行了测试-并且所显示的用户ID在NOW最高用户ID的顶部排名增加。我相信它只是在“从用户中窃取用户ID”并获取第一个结果, “ WHERE username =”未被应用。我知道获取用户名所使用的过程是可行的-就像我声明为变量并直接显示它-用户名正确-但不会显示与该用户名一起使用的useRID。

i'm looking for a way to see the exact query that is being passed to SQL while i'm running the app. 我正在寻找一种方法来查看运行应用程序时传递给SQL的确切查询。 Is there anyway in visual studio 2015 to run a SQL trace or something while in debug mode? 无论如何,Visual Studio 2015中是否在调试模式下运行SQL跟踪或其他内容?

The code is... 代码是...

Private Function GetUserID(userName As String) As Integer
    Dim userId As Integer
    Using con As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\BandDatabase.mdf;Integrated Security=True")
        Dim comm As New SqlCommand("select UserID from Users where username = userName", con)
        comm.Parameters.AddWithValue("userName", userName)
        comm.CommandType = CommandType.Text
        con.Open()
        Try
            Dim reader As SqlDataReader = comm.ExecuteReader()
            If reader.HasRows Then
                While (reader.Read)
                    userId = reader.GetInt32(0)
                End While
                reader.Close()
                Return userId
            Else
                'no user found
                Return -1
            End If
        Catch e As Exception
            Throw New Exception("An exception has occurred: " + e.Message)
        Finally
            con.Close()
        End Try
    End Using
End Function

I know the username works, becuase I can call it this way... 我知道用户名有效,因为我可以这样称呼...

userName = System.Web.HttpContext.Current.User.Identity.Name

And it picks up the right name. 它会选择正确的名称。

However, when I call my function in page load to display....(as shown below) it is only picking up the highest userID... 但是,当我在页面加载中调用我的函数以显示...时(如下图所示),它只会获取最高的用户ID ...

Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim userID As Integer = GetUserID(System.Web.HttpContext.Current.User.Identity.Name)

    'Displays a correct looking userID but not sure it is updating correctly 
    lblStudent.Text = userID
End Sub

To continue using parameters, use parameters properly. 要继续使用参数,请正确使用参数。 Note the added @ 请注意添加的@

Dim comm As New SqlCommand("select UserID from Users where username = @userName", con)
comm.Parameters.AddWithValue("@userName", userName)

Without the @ , your query is simply in the form 如果没有@ ,则查询形式为

SELECT A.B FROM A WHERE A.C = A.C

which would just return the entire table, and in your code since you iterate the results and return the last one, it would just return UserID of the last row in the table. 它将仅返回整个表,并且在您的代码中,由于您迭代结果并返回最后一个,因此它将仅返回表中最后一行的UserID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM