登录 vb.net 和 sql server 2008
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16708641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
login in vb.net and sql server 2008
提问by Husna5207
How I'm going to redirect to another page after login is successful ? and how I'm going to check if the username & password are correct or not ?
登录成功后如何重定向到另一个页面?以及我将如何检查用户名和密码是否正确?
this is my code for login :
这是我的登录代码:
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
If txtUser.Text = "" Then
MsgBox("Please Enter The Username !", MsgBoxStyle.OkOnly)
ElseIf txtPass.Text = "" Then
MsgBox("Please Enter The Password !", MsgBoxStyle.OkOnly)
End If
Dim connectionString As String = "server=''; user id=''; password=''; Database=''"
Dim conLogin As SqlClient.SqlConnection = New SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT login VALUES " _
& "(@username, @password) "
回答by Thorsten Dittmar
You need to learn SQL first: The following statement is not correct SQL.
您需要先学习SQL:以下语句不是正确的SQL。
SELECT login VALUES (@username, @password)
The statement should look like this:
该语句应如下所示:
SELECT * FROM Login WHERE UserName = @username AND Password = @password
Then you need to set the command parameters @usernameand @password.
然后你需要设置命令参数@username和@password.
I also doubt that the connection string you're using is correct. Use an SqlConnectionStringBuilderto create a correct connection string. In addition, you do not open the connection, so there's not database access possible.
我也怀疑您使用的连接字符串是否正确。使用 anSqlConnectionStringBuilder创建正确的连接字符串。此外,您没有打开连接,因此无法访问数据库。
回答by Hymany Teh
To redirect to another page, see code below
要重定向到另一个页面,请参阅下面的代码
response.redirect("abc.apsx")
回答by Denmark
You can also use datareader and this sql query
您还可以使用 datareader 和这个 sql 查询
Select count(*) from Login where UserName = @username and Password = @password
if (dr > 0) {
response.redirect('home');
}
else{
//error message
}
just search about datareader....... ...Hope it helps...
只需搜索有关数据阅读器....... ...希望它有帮助......
回答by Rohana K Amarakoon
Here is the complete code for Login Function.
这是登录功能的完整代码。
For more details visit my blog : http://uncopyrightables2011.blogspot.com/or Tweet me : @MaxRohana
有关更多详细信息,请访问我的博客:http: //uncopyrightables2011.blogspot.com/或推特我:@MaxRohana
Private Sub btnlogin_Click(sender As System.Object, e As System.EventArgs) Handles btnlogin.Click
ConnectToSQL()
End Sub
Private Sub ConnectToSQL()
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim Passowrd As String
Dim Passowrd2 As String
Dim userName As String
Try
If
'change the data source and initial catalog according to your sql server engine and data base
con.ConnectionString = "Data Source = YOUR-PC; Initial Catalog = YOUR-DB; Integrated Security = True"
con.Open()
cmd.Connection = con
'change the data fields names and table according to your database
cmd.CommandText = " SELECT UserName, Password FROM AdminDetail WHERE (UserName = '" & txtUsername.Text & "' ) AND (Password = '" & txtPassword.Text & "')"
Dim lrd As SqlDataReader = cmd.ExecuteReader()
If lrd.HasRows Then
While lrd.Read()
'Do something here
Passowrd = lrd("Password").ToString()
userName = lrd("UserName").ToString()
Passowrd2 = txtPassword.Text()
If Passowrd = Passowrd2 And userName = txtUsername.Text Then
MessageBox.Show("Logged in successfully as " & userName, "", MessageBoxButtons.OK, MessageBoxIcon.Information
)
frmMain.Show()
Me.Hide()
'Clear all fields
txtPassword.Text = ""
txtUsername.Text = ""
End If
End While
Else
MessageBox.Show("Username and Password do not match..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
'Clear all fields
txtPassword.Text = ""
txtUsername.Text = ""
End If
End If
Catch ex As Exception
MessageBox.Show("Error while connecting to SQL Server." & ex.Message)
Finally
con.Close() 'Whether there is error or not. Close the connection.
End Try
End Sub

