vb.net Asp.net (SQL) 简单登录表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25409960/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:04:34  来源:igfitidea点击:

Asp.net (SQL) Simple Login Form

asp.netvb.netformswebwebforms

提问by Alexandros Stark

I'm kinda new to asp.net but I'm learning fast, tho I cant find any good web forms tutorial for login page written in vb, I'm using the offline application tutorials to learn and I just change the commands, So i've come to a simple error for you guys, the problem is with the dsc.sqlclient, probably there's not such command, but what should I use? Thanks a lot anyway!

我对asp.net有点陌生,但我学得很快,但我找不到任何用vb编写的登录页面好的Web表单教程,我正在使用离线应用程序教程来学习,我只是更改命令,所以我给你们带来了一个简单的错误,问题出在 dsc.sqlclient 上,可能没有这样的命令,但我应该使用什么?总之非常感谢!

   Protected Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
        If Page.IsValid Then
            ' check for username & password in the database
            Dim conn As New SqlConnection("Data Source=.\sqlexpress;Initial Catalog=LoginDB;Integrated Security=True")

            ' Get the row corresponding the given username and password
            Dim strSQL As String = "Select * From Users Where Username='" + txtUname.Text + "' and Password = '" + txtPassword.Text + "'"

            Dim dsc As New SqlClient.SqlCommand(strSQL, conn)

            ' Fill the dataset 
            Dim ds As New DataSet()
            dsc.sqlclient.sqlcommand(ds, "Users")

            ' if there no entry then the user is invalid
            If ds.Tables("Users").Rows.Count = 0 Then
                Response.Redirect("Default.aspx")
            Else
                Response.Redirect("login.aspx")
            End If
        End If
    End Sub

回答by patovega

Your code should be something like this:

你的代码应该是这样的:

Protected Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
        If Page.IsValid Then
            ' check for username & password in the database
            Dim conn As New SqlConnection("Data Source=.\sqlexpress;Initial Catalog=LoginDB;Integrated Security=True")

            ' Get the row corresponding the given username and password
            Dim strSQL As String = "Select * From Users Where Username='" + txtUname.Text + "' and Password = '" + txtPassword.Text + "'"

            objConn.Open()

            ' Fill the dataset 
            Dim ds As New DataSet("Users")
            Dim daExample As New SqlDataAdapter(strSQL, objConn)
            daExample.Fill(ds, "Users2")

            ' if there no entry then the user is invalid
            If ds.Tables("Users").Rows.Count = 0 Then
                Response.Redirect("Default.aspx")
            Else
                Response.Redirect("login.aspx")
            End If
            objConn.close()
        End If
    End Sub

but you can also take this:

但你也可以这样做:

    Protected Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
                If Page.IsValid Then
                    ' check for username & password in the database
                    Dim conn As New SqlConnection("Data Source=.\sqlexpress;Initial Catalog=LoginDB;Integrated Security=True")

                    ' Get the row corresponding the given username and password
                    Dim strSQL As String = "Select * From Users Where Username='" + txtUname.Text + "' and Password = '" + txtPassword.Text + "'"
                    'I recommend not to use * in querys
                    Dim dsc As New SqlClient.SqlCommand(strSQL, conn)

                    Dim dr As SqlDataReader
                    dr = dsc.ExecuteReader()

                     If dr.HasRows = True Then
                        Response.Redirect("Default.aspx")
                     Else
                         Response.Redirect("login.aspx")
                     End If
                End If
       End Sub

回答by Alexandros Stark

Thanks a lot guys, this is the correct answer tho, kbworkshophelped me a lot!

非常感谢大家,这是正确的答案,kbworkshop帮了我很多!

For anyone wanna know this is the code

对于任何想知道这是代码的人

Protected Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
        If Page.IsValid Then
            ' check for username & password in the database
            Dim conn As New SqlConnection("Data Source=.\sqlexpress;Initial Catalog=LoginDB;Integrated Security=True")

            ' Get the row corresponding the given username and password
            Dim strSQL As String = "Select * From Users Where Username='" + txtUname.Text + "' and Password = '" + txtPassword.Text + "'"
            'I recommend not to use * in querys
            Dim dsc As New SqlClient.SqlCommand(strSQL, conn)
            conn.Open()
            Dim dr As SqlDataReader
            dr = dsc.ExecuteReader()

            If dr.HasRows = True Then
                Response.Redirect("Default.aspx")
            Else
                Response.Redirect("login.aspx")
            End If
            conn.Close()
        End If
    End Sub

回答by HelenPR

PLEASEdon't create your SELECT statement by pasting text together.

不要通过将文本粘贴在一起来创建 SELECT 语句。

Ugh. Never do that.

啊。永远不要那样做。

You just allow anyone to use "SQL Injection" to log in (and worse) without a password.

您只是允许任何人使用“SQL 注入”在没有密码的情况下登录(甚至更糟)。

回答by user12366174

    Imports System.Data
    Imports System.Data.SqlClient

Partial Class log
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim cn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=F:\WebSite1\App_Data\Database.mdf;Integrated Security=True;User Instance=True")
        Dim log As String = "SELECT * FROM login WHERE userid='" & TextBox1.Text & "' AND password='" & TextBox2.Text & "'"

        Session("user") = TextBox1.Text

        Dim cmd As New SqlCommand(log, cn)
        Dim dr As SqlDataReader

        cn.Open()
        dr = cmd.ExecuteReader()

        If dr.HasRows = True Then
            Response.Redirect("showdata.aspx")
        Else
            Response.Redirect("log.aspx")

        End If

    End Sub

End Class

回答by vmgmail

You can use SqlDataAdapter class to fill your dataset:

您可以使用 SqlDataAdapter 类来填充您的数据集:

SqlConnection conn = new SqlConnection("My ConnectionString");
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = SQL;
da.SelectCommand = cmd;
DataSet ds = new DataSet();

conn.Open();
da.Fill(ds);
conn.Close();

VB.Net:

VB.Net:

Dim conn As New SqlConnection("My ConnectionString")
Dim da As New SqlDataAdapter()
Dim cmd As SqlCommand = conn.CreateCommand()
cmd.CommandText = SQL
da.SelectCommand = cmd
Dim ds As New DataSet()

conn.Open()
da.Fill(ds)
conn.Close()