简体   繁体   English

如何修复“FormatException:输入字符串格式不正确”

[英]How do i fix a “FormatException: Input string was not in a correct format”

I am doing some coding for my school project and I'm trying to create a program that uses filters and displays a set of items based on those queries.我正在为我的学校项目做一些编码,我正在尝试创建一个使用过滤器并根据这些查询显示一组项目的程序。 However, when writing the SQL statement, i get a FormatException.但是,在编写 SQL 语句时,我得到一个 FormatException。

Here is my line of code:这是我的代码行:

Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM partList WHERE [price] = '" & filterMinPrice And filterMaxPrice & "'AND [size] = '" & filterMicroATX And filterATX & "'AND [usability] = '" & filterHomeUse And filterSemiIntensive And filterHighIntensive)

and this is the error i receive:这是我收到的错误:

System.InvalidCastException: 'Conversion from string "SELECT * FROM partList WHERE [pr" to type 'Long' is not valid.' System.InvalidCastException:“从字符串“SELECT * FROM partList WHERE [pr”到类型“Long”的转换无效。” FormatException: Input string was not in a correct format. FormatException:输入字符串的格式不正确。

Where am I going wrong here?我在哪里错了?

I have demonstrated how to use parameters.我已经演示了如何使用参数。 Your main problem was in your logic in the query string.您的主要问题在于查询字符串中的逻辑。 Each section of the Where clause separated by an And or Or must evaluate to True or False.AndOr分隔的Where子句的每个部分必须计算为 True 或 False。 Think about it.想想看。 The price couldn't equal both the Max and Min.价格不能同时等于 Max 和 Min。 You will have to check the database to see the correct datatypes of the fields because I had to guess.您将不得不检查数据库以查看字段的正确数据类型,因为我不得不猜测。 The Using...End Using block ensures that the connection is closed and disposed and the command is disposed. Using...End Using块确保连接被关闭和释放,并且命令被释放。

Private ConStr As String = "Your connection string"
Private Function GetPartData(filterMinPrice As Decimal, filterMaxPrice As Decimal, filterMicroATX As Integer, filterATX As Integer, filterHomeUse As String, filterSemiIntensive As String, filterHighIntensive As String) As DataTable
    Dim dt As New DataTable
    Dim sql = "SELECT * FROM partList 
                WHERE [price] > @MinPrice 
                And [price] < @MaxPrice 
                AND [size] > @MicroATX 
                And [size] < @ATX 
                AND [usability] = @HomeUse 
                Or [usability] = @SemiIntensive 
                Or [usability] = @HighIntensive;"
    Using con As New OleDbConnection(ConStr),
            cmd As New OleDbCommand(sql, con)
        With cmd.Parameters
            .Add("@MinPrice", OleDbType.Decimal).Value = filterMinPrice
            .Add("@MaxPrice", OleDbType.Decimal).Value = filterMaxPrice
            .Add("@MicroATX", OleDbType.Integer).Value = filterMicroATX
            .Add("@ATX", OleDbType.Integer).Value = filterATX
            .Add("@HomeUse", OleDbType.VarChar).Value = filterHomeUse
            .Add("@SemiIntensive", OleDbType.VarChar).Value = filterSemiIntensive
            .Add("@HighIntensive", OleDbType.VarChar).Value = filterHighIntensive
        End With
        con.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    Return dt
End Function

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复System.FormatException:&#39;输入字符串的格式不正确&#39; - How to fix System.FormatException: 'Input string was not in a correct format' System.FormatException:输入字符串的格式不正确 - System.FormatException: Input string was not in a correct format Double ParseDouble - 输入字符串格式不正确 - 如何修复? - Double ParseDouble - Input string was not in a correct format - How to Fix? System.FormatException: '输入字符串的格式不正确。' VB.NET - System.FormatException: 'Input string was not in a correct format.' VB.NET Dim b As Integer = Integer.Parse(“1”) 导致“System.FormatException:'输入字符串格式不正确。'” - Dim b As Integer = Integer.Parse(“1”) resulting in “System.FormatException: 'Input string was not in a correct format.'” CommanArgument传递数字未处理的异常System.FormatException:输入字符串的格式不正确 - CommanArgument passing numbers Unhandled excetion System.FormatException: Input string was not in a correct format System.FormatException:&#39;输入字符串的格式不正确。 转换为十进制时 - System.FormatException: 'Input string was not in a correct format.' when converting to Decimal 收到错误“System.FormatException:'输入字符串的格式不正确。'”VB - Getting the error "System.FormatException: 'Input string was not in a correct format.'" VB 输入的字符串格式不正确? 但它是 - Input string was not in a correct format? but it is 输入的字符串格式不正确 - Input string was not in a correct format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM