我有一个用VB.NET编写的遗留解决方案。它使用ODBC写入DB2数据库。有一个充满数字值的文本框,然后我使用ToString来转换在那里写入的任何内容,并使用ExecuteNonQuery将其写入数据库,因为该字段是CHAR(10)类型的,使用AnyCPU编译时会产生Arithmetic operation resulted in an overflow问题,但是在32 bits.中编译时不会发生这种情况,这是什么原因,因为我使用它作为字符串使用?
编辑:
Public Sub ExecuteTransaction(ByVal connectionString As String)
Using connection As New OdbcConnection(connectionString)
Dim command As New OdbcCommand()
command.Connection = connection
Try
connection.Open()
command.Connection = connection
command.CommandText = "Update QS36f.table set Cat= 'F' where Num= '" & Me.txt2.ToString.Trim &"'"
command.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Using
End Sub发布于 2022-05-15 07:21:40
当我在12345中输入TextBox,然后执行以下操作:
Dim input As String = Me.txt2.ToString.Triminput的内容不是所需的"12345",而是TextBox及其内容的字符串表示形式,即:"System.Windows.Forms.TextBox, Text: 12345"
若要只获取内容,请使用.Text
Dim input As String = Me.txt2.Text.Trim在将输入传递给查询之前,我还会尝试立即将其转换为整数(假设所需的输入是整数):
Dim input As Integer
If Not Integer.TryParse(Me.txt2.Text, input) Then
MessageBox.Show("Input could not be converted to integer")
Exit Sub
End If如果这不能解决这个问题(正如user18387401所提到的),那么使用参数而不是字符串连接可能会有帮助:
command.CommandText = "Update QS36f.table set Cat= 'F' where Num= @Num"
Dim param As New OdbcParameter("Num", OdbcType.VarChar)
param.Value = Me.txt2.Text.Trim
command.Parameters.Add(param)编辑:不要使用Parameters.AddWithValue。
https://stackoverflow.com/questions/72240675
复制相似问题