请帮我解决这个问题。 [英] Please help me out for that problem.

查看:105
本文介绍了请帮我解决这个问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新按钮不工作数据类型不匹配错误弹出



我尝试过:



Private Sub btnupdate_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnupdate.Click

尝试

Dim str作为字符串

cn.Open()

str =更新[联系方式]设置[QuotesId] ='& txtquotesid.Text& ',[日期] ='& date1.Text& ',[客户名称] ='& txtname.Text& ',[公司] ='& txtcompany.Text& ',[地址] ='& txtaddress.Text& ',[Town] ='& txttown.Text& ',[州] ='& txtdstate.Text& ',[Zip] ='& txtzip.Text& ',[手机] ='& txtmobile.Text& ',[电话] ='& txtphone.Text& ',[Ext] ='& txtext.Text& ',[电子邮件] ='& txtemail.Text& ',[StartDate] ='& startdate.Text& ',[EndDate] ='& enddate.Text& ',[OrAdd] ='& txtoadd.Text& ',[OrTown] ='& txtotown.Text& ',[OrState] ='& txtostate.Text& ',[OrZip] ='& txtozip.Text& ',[OrTime] ='& starttime.Text& ',[DestAdd] ='& txtdadd.Text& ',[DestTown] ='& txtdtown.Text& ',[DestState] ='& txtdstate.Text& ',[DestZip] ='& txtdzip.Text& ',[DestTime] ='& endtime.Text& ',[注意] ='& txtnotes.Text& ',[Price] ='& txtprice.Text& ',[价格日] ='& txtdayprice.Text& 'WHERE([QuotesId] ='& txtquotesid.Text&')

cmd =新OleDbCommand(str,cn)

i = cmd.ExecuteNonQuery ()

如果i> = 0那么

MsgBox(数据修改)

否则

MsgBox (对不对的事)

结束如果

ds.Clear()

da.Fill(ds,联系方式)

Get_data()

Catch ex As Exception

MsgBox(ex.Message)

结束尝试

cn.Close()

End Sub

结束类

Update Button is not working data type mismatch error is pop up

What I have tried:

Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
Try
Dim str As String
cn.Open()
str = "Update [Contact] set [QuotesId] ='" & txtquotesid.Text & "',[Date] ='" & date1.Text & "',[Customer Name] ='" & txtname.Text & "',[Company] ='" & txtcompany.Text & "',[Address] ='" & txtaddress.Text & "',[Town] ='" & txttown.Text & "',[State] ='" & txtdstate.Text & "',[Zip] ='" & txtzip.Text & "',[Mobile] ='" & txtmobile.Text & "',[Phone] ='" & txtphone.Text & "',[Ext] ='" & txtext.Text & "',[Email] ='" & txtemail.Text & "',[StartDate] ='" & startdate.Text & "',[EndDate] ='" & enddate.Text & "',[OrAdd] ='" & txtoadd.Text & "',[OrTown] ='" & txtotown.Text & "',[OrState] ='" & txtostate.Text & "',[OrZip] ='" & txtozip.Text & "',[OrTime] ='" & starttime.Text & "',[DestAdd] ='" & txtdadd.Text & "',[DestTown] ='" & txtdtown.Text & "',[DestState] ='" & txtdstate.Text & "',[DestZip] ='" & txtdzip.Text & "',[DestTime] ='" & endtime.Text & "',[Notes] ='" & txtnotes.Text & "',[Price] ='" & txtprice.Text & "',[Price Day] ='" & txtdayprice.Text & "' WHERE ([QuotesId] = '" & txtquotesid.Text & "')"
cmd = New OleDbCommand(str, cn)
i = cmd.ExecuteNonQuery()
If i >= 0 Then
MsgBox("Data Modified")
Else
MsgBox("Sorry Something Wrong")
End If
ds.Clear()
da.Fill(ds, "Contact")
Get_data()
Catch ex As Exception
MsgBox(ex.Message)
End Try
cn.Close()
End Sub
End Class

推荐答案

检查每个字段是否为是不是字符串。



不是你问题的解决方案,而是你遇到的另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]
Check if every field is a string or not.

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]


永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



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

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

--'

其他一切都是评论。

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



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



这样做的可能性实际上也可以解决你的问题 - 因为它抱怨的是,您提供的数据类型不适合该列。通过检查用户输入并将其转换为标准类型而不是假设用户始终是正确的 - 而且他远非如此,就像我们所有人一样 - 您在错误到达数据库之前就会发现错误,如果没有被捕获则会让您陷入困境作为现在的SQL错误。

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?

The chances are that doing that will actually fix your problem as well - since what it is complaining about is that the type of data you gave it doesn't fit in that column. By checking and converting user input to standard types rather than assuming the user is always right - and he is far from that, just like us all - you catch the errors before they reach the DB and mess you up later if they aren't caught as an SQL error as they are being now.


这篇关于请帮我解决这个问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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