从字符串“类型”转换输入'整数'是无效的。'请帮我

编程入门 行业动态 更新时间:2024-10-11 05:20:48
本文介绍了从字符串“类型”转换输入'整数'是无效的。'请帮我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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[^]

更多推荐

从字符串“类型”转换输入'整数'是无效的。'请帮我

本文发布于:2023-10-26 17:52:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530900.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:帮我   整数   字符串   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!