Private Sub OKbt1_Click(sender As Object, e As EventArgs) Handles OKbt1.Click Call Connect() ' [ connection to module ]' Dim Reader As SqlDataReader Try 'selecting DATA from DATABASE Dim query As String query = "select * from uinfo where password = '" & PASStb2.Text & "' " command = New SqlCommand(query, sqlConn) 'Dim command As New SqlCommand("select * from uinfo where password = '" & PASStb2.Text & "'", sqlConn) 'Dim dt As New DataTable 'Dim adapter As New SqlDataAdapter(command) 'adapter.Fill(dt) Reader = command.ExecuteReader Dim count As Integer count = 0 While Reader.Read count = count + 1 End While If count = 1 Then Dim usertype = Reader.GetString("Type") If usertype = "admin" Then 'MsgBox("username and password are correct") MAIN_MENU.Show() For a = 0 To 500 Next Me.Hide() sqlConn.Close() sqlConn.Dispose() ElseIf usertype = "user" Then For a = 0 To 500 Next Me.Hide() 'MsgBox("username and password are correct") USERMENU.Show() End If ElseIf count > 1 Then MsgBox("username and password are duplicate") Else MsgBox("username and password are not correct") End If sqlConn.Close() Catch ex As SqlException MsgBox(ex.Message) Finally sqlConn.Dispose() End Try End Sub
我尝试了什么: i尝试将其改为双倍但同样的错误d Sub 错误就像这样
What I have tried: i tried to change it to double but same error d Sub the error goes like this "
System.InvalidCastException: 'Conversion from string "Type" to type 'Integer' is not valid.'"推荐答案
这么短的代码中有很多不好的想法... 不要那样做!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。 连接字符串时会导致问题,因为SQL会收到如下命令: So many bad ideas in such a short piece of code... Don't do it like that! 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. Use Parametrized queries instead. When you concatenate strings, you cause problems because SQL receives commands like: SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:
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;--'哪个SQL看作三个单独的命令:
Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x';完全有效的SELECT
A perfectly valid SELECT
DROP TABLE MyTable;完全有效的删除表格通讯和
A perfectly valid "delete the table" command
--'其他一切都是评论。 所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。 所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗? 当你在整个应用程序中修复了这个问题后,你可以看看你需要做出的下一个重大改变:切勿以明文形式存储密码 - 这是一个主要的安全风险。有关如何在此处执行此操作的信息:密码存储:如何做到这一点。 [ ^ ] - 代码在C#中,但它非常明显,如果你被卡住,在线转换器可以提供帮助。 只是为了让你了解一下如何关注文本基于密码: Code Crime 1 [ ^ ]
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 when you have fixed that throughout your app, you can look at the next big change you need to make: 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 obvious, and online converters can help if you are stuck. Just to give you an idea in how little regard text based passwords are held: Code Crime 1[^]
更多推荐
从字符串“类型”转换输入'整数'是无效的。'请帮我
发布评论