在VB.NET中登录表单 [英] Log in form in VB.NET

查看:96
本文介绍了在VB.NET中登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。

i有这个代码,它的工作(种类)。

我得到这一行的错误:

 Form2.Label2。 Text = reader(2).ToString 



错误:

没有数据时读取的次数无效



为什么它说没有数据?我有数据库中的所有数据吗?



有人可以帮我纠正这段代码吗?

谢谢..





 我的尝试: 

< pre lang = vb>
< pre>
Dim connString As String = ConfigurationManager.ConnectionStrings( connectionstring)。ConnectionString
Dim conn 作为 SqlConnection(connString)
conn.Open ()
Dim comm As SqlCommand( SELECT用户名,密码,类型FROM users WHERE username ='& TextBox1。文字& 'AND Password ='& TextBox2.Text& ',conn)
Dim 阅读器 As SqlDataReader
reader = comm.ExecuteReader

Dim count < span class =code-keyword>作为 整数
count = 0
while reader.Read
count = count + 1
< span class =code-keyword>结束 虽然
如果 count = 1 然后
MessageBox.Show( 用户名和密码正确


Form2.Show()


Form2.Label1.Text = .TextBox1.Text
Form2.Label2.Text = reader( 2 ) .ToString
ElseIf count> 1 然后
MessageBox.Show( 用户名和密码重复
其他
MessageBox.Show ( 用户名和密码错误

结束 如果

解决方案

那么,是的 - 你会的。

让我撕掉一些代码,这样更容易看到:

虽然reader.Read 
count = count + 1
结束
如果count = 1那么
...
Form2.Label2.Text = reader(2).ToString

你循环通过所有行来计算它们。所以在循环之后, reader 保证不会查看一行 - 因为如果它是 reader.Read 不会返回false!

所以 reader 没有连续,如果你尝试访问行数据,那里就没有了看看!结果你得到一个错误。



但这是非常危险的代码,特别是在登录表单上!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  Baker' s Wood ' < span class =code-string>  

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  x';  DROP   MyTable;   -   ' 

哪个SQL看作三个单独的命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  x'; 

完全有效的SELECT

  DROP   TABLE  MyTable; 

完全有效的删除表格通讯和

   -   ' 

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你经常定期备份,不是吗?



这意味着你需要优先考虑你的整个应用程序,并在整个过程中解决这个问题:一个和你的数据库有风险!



另一个问题是你处理密码的方式,这既危险又(因为GDPR)可能意味着你要负责如果欧盟公民试图使用您的系统,则因疏忽处理用户数据而被起诉...

切勿以明文形式存储密码 - 这是一个主要的安全风险。有关如何在此处执行此操作的信息:密码存储:如何做到这一点。 [ ^ ] - 代码在C#中,但它非常简单,如果你无法应对,则tehre是在线转换器:代码转换器C#到VB和VB到C# - Telerik [ ^ ]


hello.
i have this code,,its work (kind of).
im getting error with this line:

Form2.Label2.Text = reader(2).ToString


error :

Invalid attempt to read when no data is present


why its says "no data"? i have all data in database?

can someone helpo me to correct this code?
thank you ..


What I have tried:

<pre lang="vb">
 <pre>  
Dim connString As String = ConfigurationManager.ConnectionStrings("connectionstring").ConnectionString
        Dim conn As New SqlConnection(connString)
        conn.Open()
        Dim comm As New SqlCommand("SELECT username, Password,type   FROM users WHERE username='" & TextBox1.Text & "' AND Password='" & TextBox2.Text & "'", conn)
        Dim reader As SqlDataReader
        reader = comm.ExecuteReader

        Dim count As Integer
        count = 0
        While reader.Read
            count = count + 1
        End While
        If count = 1 Then
            MessageBox.Show("username and password are correct")


            Form2.Show()


            Form2.Label1.Text = Me.TextBox1.Text
            Form2.Label2.Text = reader(2).ToString
        ElseIf count > 1 Then
                MessageBox.Show("username and password are duplicated")
            Else
                MessageBox.Show("username and password are wrong")

        End If

解决方案

Well, yes - you will.
Let me rip some code out so it's easier to see:

        While reader.Read
            count = count + 1
        End While
        If count = 1 Then
...
            Form2.Label2.Text = reader(2).ToString

You loop through all rows to count them. So after the loop, reader is guaranteed to not be looking at a row - because if it was then reader.Read would not have returned false!
So reader isn't on a row, and if you try access row data, there isn't any there to look at! And you get an error as a result.

But that is extremely dangerous code, particularly so on a login form! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:

SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

A perfectly valid SELECT

DROP TABLE MyTable;

A perfectly valid "delete the table" command

--'

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And that means you need to go through your whole app as a matter of priority and fix that throughout: leave just one and your database is at risk!

The other problem is the way you are handling passwords, which is both dangerous and (since GDPR) can mean you are liable for prosecution for negligent handling of user data if a EU citizen tries to use your system ...
Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^] - the code is in C#, but it's pretty simple, and tehre are online converters if you can't cope: Code Converter C# to VB and VB to C# – Telerik[^]


这篇关于在VB.NET中登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆