我是新手编写的,所以请原谅我,如果我的问题看起来很基本,但是我在从While循环中提取变量时遇到了一些麻烦,以便随后使用我的SQL查询结果进行验证。
以下脚本是.aspx表单上登录按钮的事件处理,处理将在相关MSSQL数据库中列出的电子邮件和登录字段:
Public Class _Default
Inherits System.Web.UI.Page
Protected Sub submit_Click(sender As Object, e As EventArgs) Handles submit.Click
Dim Column1 As String
Dim Column2 As String
Dim SQL = "SELECT * FROM Logins WHERE Email='" & email.Text & "' AND Password='" & password.Text & "'"
Dim oSqlDataReader As System.Data.SqlClient.SqlDataReader = Nothing
Using oSqlConnection As New System.Data.SqlClient.SqlConnection("SERVER=[Server Name];UID=[User];PWD=[Pass];DATABASE=[Database Name]")
oSqlConnection.Open()
Using oSqlCommand As New System.Data.SqlClient.SqlCommand(SQL, oSqlConnection)
oSqlDataReader = oSqlCommand.ExecuteReader
While oSqlDataReader.Read
Column1 = oSqlDataReader(name:="Email")
Column2 = oSqlDataReader(name:="Password")
End While
End Using
oSqlConnection.Close()
End Using
If "Column 1 etc."
End if
End Sub
End Class
据我所知,我的代码没有错误,但每次我尝试创建一个If语句时,我的变量Column 1 and Column 2
都是未声明的,这使得它们毫无用处。
如果有人可以为我的代码或缺失区域提供正确的布局,并解释我出错的地方,那就太棒了。
答案 0 :(得分:0)
如果您在循环中移动If
块,您是否更接近您期望的行为?
Protected Sub submit_Click(sender As Object, e As EventArgs) Handles submit.Click
Dim Column1 As String
Dim Column2 As String
Dim SQL = "SELECT * FROM Logins WHERE Email='" & email.Text & "' AND Password='" & password.Text & "'"
Dim oSqlDataReader As System.Data.SqlClient.SqlDataReader = Nothing
Using oSqlConnection As New System.Data.SqlClient.SqlConnection("SERVER=[Server Name];UID=[User];PWD=[Pass];DATABASE=[Database Name]")
oSqlConnection.Open()
Using oSqlCommand As New System.Data.SqlClient.SqlCommand(SQL, oSqlConnection)
oSqlDataReader = oSqlCommand.ExecuteReader
While oSqlDataReader.Read
Column1 = oSqlDataReader(name:="Email")
Column2 = oSqlDataReader(name:="Password")
If "Column 1 etc....."
End if
End While
End Using
oSqlConnection.Close()
End Using
End Sub
答案 1 :(得分:0)
可能是您的查询没有返回任何行,或者返回的值是dbNull或没有。如果合适的话,我会检查返回的数据和错误。
尝试直接对数据库运行查询。你回来了吗?
为避免错误,您可以将字符串声明为String.empty
Dim Column1 As String = String.empty
或者,在if语句中使用它时检查无效:
If Column1 Is Not Nothing AndAlso ...
答案 2 :(得分:0)
不要使用字符串连接来构建sql查询。而是使用sql-parameters来防止sql注入和其他问题。
我必须承认我不知道这种语法:oSqlDataReader(name:="Email")
。使用以下:
Dim email As String
Dim password As String
Dim sql = "SELECT * FROM Logins WHERE Email=@Email AND Password=@Password"
Using oSqlConnection As New System.Data.SqlClient.SqlConnection("SERVER=[Server Name];UID=[User];PWD=[Pass];DATABASE=[Database Name]")
Using oSqlCommand As New System.Data.SqlClient.SqlCommand(sql, oSqlConnection)
oSqlCommand.Parameters.Add("@Email", SqlDbType.VarChar).Value = email.Text
oSqlCommand.Parameters.Add("@Password", SqlDbType.VarChar).Value = password.Text
oSqlConnection.Open()
Using oSqlDataReader = oSqlCommand.ExecuteReader()
If oSqlDataReader.Read() Then
Dim emailColIndex = oSqlDataReader.GetOrdinal("Email")
Dim pwdColIndex = oSqlDataReader.GetOrdinal("Password")
email = oSqlDataReader.GetString(emailColIndex)
password = oSqlDataReader.GetString(pwdColIndex)
End If
End Using
End Using
End Using ' oSqlConnection.Close() not needed with using '
If email IsNot Nothing AndAlso password IsNot Nothing Then
End If
但是,您应该实现一个Login
类,而不是初始化两个字符串变量,您可以从该方法初始化并返回该类。您不想知道电子邮件和密码,因为您已经拥有它们。
由于这是ASP.NET,我建议查看功能强大且具有学习曲线的可用membership provider,但绝对值得花时间。