简体   繁体   English

无法将vb.net项与访问号进行比较

[英]Can't compare vb.net item with access number

Edit: For anyone facing this problem, don't miss the tips about only using parameters instead of inserting the values directly into the SQL queries. 编辑:对于遇到此问题的任何人,请不要错过仅使用参数而不是将值直接插入SQL查询的提示。


i'm facing a big problem with my vb.net project, i'm stuck with this for a week 我的vb.net项目面临一个大问题,我坚持了一个星期

i've a combobox item that i need to compare with an access number which is my database to retrieve some information, but i just got an error, no matter what format i convert my combobox item, it says my datatype is inconpatible with the expression 我有一个组合框项目,我需要与访问号码进行比较,这是我的数据库,以检索一些信息,但我只是有一个错误,无论我转换我的组合框项目的格式,它说我的数据类型与表达式不相称

Here's one of the SQL queries from my code: 这是我的代码中的一个SQL查询:

Dim dt1 As New DataTable 

'This query select some itens from a row that match with the selected combobox number  

Dim find1 As New OleDb.OleDbDataAdapter("SELECT Product, Number," _ 
& " Customer, Quantity, ProductionDate, AskDay, Pack, Company FROM RegOPE" _ 
& " WHERE Number ='" & CInt(mycombobox.SelectedItem) & "'", cn)

'Ive tried SelectedItem, Item, Text, SelectedValue...  
'For conversion i tried parse, tryparse, conversion...  

cn.Open() 'Opens database connection  
find1.Fill(dt1)  <- I got the error here  
cn.Close() 'Close database connect  
mydatagrid.DataSource = dt1 'Show the result in datagridview  

In your WHERE clause have you tried to remove the quotes ? 在您的WHERE子句中,您是否尝试删除引号? They are not required if you are looking for a number. 如果您正在寻找一个号码,则不需要它们。

number criteria should be without quote 数字标准应该没有引用

Dim find1 As New OleDb.OleDbDataAdapter("SELECT Product, Number, " _
                                      & "Customer, Quantity, ProductionDate, AskDay, Pack, Company FROM RegOPE " _
                                      & "WHERE Number =" & CInt(mycombobox.SelectedItem), cn)

But better always use parameters: 但最好总是使用参数:

Dim comm = New OleDb.OleDbCommand("SELECT Product, Number, " _
                                      & "Customer, Quantity, ProductionDate, AskDay, Pack, Company FROM RegOPE " _
                                      & "WHERE Number =?", cn)

comm.Parameters.AddWithValue("unusedName", mycombobox.SelectedItem)

Dim find1 As New OleDb.OleDbDataAdapter(comm)

First, I must mention that you really ought to be using parameters. 首先,我必须提到你真的应该使用参数。 You should not concatenate the values directly into the SQL command string like that. 您不应该将值直接连接到SQL命令字符串中。 Your SQL command string should simply contain parameter name placeholders for those values and then you should specify the values for those parameters separately. 您的SQL命令字符串应该只包含这些值的参数名称占位符,然后您应该分别指定这些参数的值。

However, if you are going to concatenate the value with the command like that, the command is a string, not an integer. 但是,如果要将该值与该命令串联,则该命令是字符串,而不是整数。 It makes little sense to use CInt to convert the item to an integer just before concatenating it with a string (which requires first converting it from the integer into a string). 在将项目与字符串连接之前使用CInt将项目转换为整数(这需要首先将其从整数转换为字符串)是没有意义的。 It would make more sense to simply call ToString to convert it to a string, instead of CInt . 简单地调用ToString将其转换为字符串而不是CInt更有意义。 Also, if the Number column in your database is typed as a number, rather than as text, then you should not be surrounding the value with quotes. 此外,如果数据库中的“ Number列被键入为数字而不是文本,则不应使用引号括起该值。

I recommend trying this: 我建议试试这个:

Dim find1 As New OleDb.OleDbDataAdapter("SELECT Product, Number," _ 
    & " Customer, Quantity, ProductionDate, AskDay, Pack, Company FROM RegOPE" _ 
    & " WHERE Number =" & mycombobox.SelectedItem.ToString(), cn)

Although, recommend is to strong a word, since I would never recommend doing it that way in the first place. 虽然, 推荐是坚持一句话,因为我绝不会建议首先这样做。 Use parameterized queries! 使用参数化查询!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM