简体   繁体   English

使用ASP.NET(VB.NET)将文本框字段与服务器端数据库进行比较

[英]Compare Text Box field to Server-side DB using ASP.NET (VB.NET)

I'm currently working on producing a quoting system that will run with a webform front end and be populated using a database on a server (ASP.NET). 我目前正在开发一种报价系统,该系统将与Webform前端一起运行,并使用服务器(ASP.NET)上的数据库进行填充。

I'm reasonably competent in how databases work and how to write HTML/CSS but still a novice at linking the two together by running VB.NET at the server using code behind. 我在数据库的工作方式以及如何编写HTML / CSS方面具有相当的能力,但是对于通过使用后面的代码在服务器上运行VB.NET将两者链接在一起仍然是一个新手。

So far I've created an .aspx page and used the code below to create my form: 到目前为止,我已经创建了一个.aspx页面,并使用下面的代码创建了表单:

    <form id="LoginForm" runat="server" method="post">
            <div class="row email">
                <asp:TextBox ID="email" placeholder="Email" TextMode="Email" runat="server"></asp:TextBox>
            </div>

            <div class="row pass">
                <asp:TextBox ID="password" placeholder="Password" TextMode="Password" runat="server"></asp:TextBox>
            </div>

            <asp:Button ID="submit" runat="server" Text="Submit" OnClick="submit_Click" />

        </form>

I have a database I want to compare the email and password to on the server. 我有一个数据库,我想比较服务器上的电子邮件和密码。 I understand this would be on click but what is the easiest method of going about this? 我知道可以点击一下,但是最简单的方法是什么?

So far I have linked "CodeBehind" and a sub ready for 到目前为止,我已经链接了“ CodeBehind”和一个准备就绪的子

Protected Sub submit_Click(sender As Object, e As EventArgs) Handles submit.Click

As far as I've been able to understand, I need to open a database connection? 据我所了解,我需要打开数据库连接吗? Use ADO? 使用ADO? Implement a SQL query somehow? 以某种方式实现SQL查询?

There's a lot of these terms on various forums and sites but much detail on what they do or how they interact for beginners like myself. 在各种论坛和站点上有很多这样的术语,但是对于像我这样的初学者来说,它们的作用或交互方式有很多细节。 Any help on what basic methods to take would be fantastic or any links to sites that explained all this would be great too. 关于采取什么基本方法的任何帮助都将是极好的,或者任何解释这些步骤的站点链接也将非常有用。 It doesn't need to be hugely secure as this project is for learning purposes more than a robust system. 它不需要非常安全,因为该项目用于学习的目的不仅仅是强大的系统。

NOTE: I did produce a similar form on Access where I treat the textbox fields as variables and ran a SQL query with them inside. 注意:我确实在Access上生成了类似的表格,其中将文本框字段视为变量,并在其中运行了SQL查询。 If correct it allowed access if false it asked the user to try again. 如果正确,则允许访问(如果为false),则要求用户重试。 Would this be a similar method? 这会是类似的方法吗?

Here's a simple example on how you could do it, there are various others like using an ASP.NET Memberhsip provider: 这是一个有关如何实现的简单示例,还有其他各种示例,例如使用ASP.NET Memberhsip提供程序:

Protected Sub submit_Click(sender As Object, e As EventArgs) Handles submit.Click
    Dim sql = "SELECT u.* FROM dbo.Users u WHERE u.UserName=@Username AND u.PassWord=@Password"
    Using con As New SqlConnection(My.Settings.ConnectionString)
        Using cmd As New SqlCommand(sql, con)
            cmd.Parameters.Add("@Username", SqlDbType.VarChar, 100).Value = email.Text
            cmd.Parameters.Add("@Password", SqlDbType.VarChar, 50).Value = password.Text
            con.Open()
            Using rd = cmd.ExecuteReader()
                Dim userExists As Boolean = rd.HasRows
                If userExists Then
                    While rd.Read()
                        Dim dateBirth = rd.GetDateTime(rd.GetOrdinal("DateOfBirth"))
                        ' .... '
                    End While
                End If
            End Using
        End Using
    End Using
End Sub

You could also use a SqlDataAdapter to fill a DataTable . 您还可以使用SqlDataAdapter填充DataTable It's also a good idea to implement a User class and use the loop above to fill a List(Of User) . 实施User类并使用上面的循环填充List(Of User)也是一个好主意。

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

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