无法将参数值从字符串转换为我得到的int32错误 [英] Failed to convert parameter value from a string to a int32 error I m getting
问题描述
Dim da As New SqlDataAdapter
da.InsertCommand = cmd
da.InsertCommand.ExecuteNonQuery()'Here im geeting error
我尝试了什么:
i想要解决这个问题>
im在数据库中插入数据并获取错误
Dim da As New SqlDataAdapter
da.InsertCommand = cmd
da.InsertCommand.ExecuteNonQuery() 'Here i m geeting error
What I have tried:
i want solution of this
i m inserting data in database and getting error
推荐答案
查看错误消息:
Look at the error message:
Failed to convert parameter value from a string to a int32
它说我在这里期待一个整数,你给了我一个字符串它不包含整数值。
所以使用调试器查看 cmd
并查看具体内容您正在作为命令传递 - 并且由于该代码不使用参数,因此您的代码可能对SQL注入是开放的。
永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。
连接字符串时会导致问题,因为SQL会收到如下命令:
It's saying "I expected an integer here, and you gave me a string that doesn't contain an integer value."
So look at cmd
using the debugger and see exactly what you are passing as the command - and since that code doesn't use parameters, the chances are that your code is wide open to SQL Injection.
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
--'
其他一切都是评论。
所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。
所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?
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?
这篇关于无法将参数值从字符串转换为我得到的int32错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!