简体   繁体   English

VB.NET 数据库读取错误

[英]VB.NET Database reading error

I have this SQL sentence for retrieving a specific role (by the column RoleID) from the table Roles:我有这个 SQL 语句用于从表 Roles 中检索特定角色(通过 RoleID 列):

DBBroker.getInstance.read("SELECT * FROM Roles WHERE RoleID='" & role.roleID & "';")

The thing is that when my program runs the sentence, I obtain this error:问题是当我的程序运行这句话时,我得到了这个错误:

获得的错误

I work with a Microsoft Access Database in which RoleID is defined as Autonumber while in my program it is defined as String.我使用 Microsoft Access 数据库,其中 RoleID 被定义为自动编号,而在我的程序中它被定义为字符串。 Anyway I tried changing Types but it still fails.无论如何,我尝试更改类型,但仍然失败。

I have too much code in the program so I cannot include it here, but I'm open for any requests regarding a specific part of the program.我的程序中有太多代码,所以我不能在这里包含它,但我愿意接受有关程序特定部分的任何请求。

Thanks谢谢

By the way I worked before on another similar database and the exact same clause did work indeed.顺便说一句,我之前在另一个类似的数据库上工作过,完全相同的子句确实有效。

Though you solved your problem,i am answering this just to describe and help guys who visit this page in future虽然你解决了你的问题,但我回答这个只是为了描述和帮助将来访问这个页面的人

Here's a one line explanation of the issue :这是该问题的单行解释:

The data type you set for the column in the table of the database is different than the type of value you are passing您为数据库表中的列设置的数据类型与您传递的值类型不同

For example : In simple words, you have a word and a number , can you add them ?例如:在简单的单词中,您有一个单词和一个数字,您可以将它们相加吗? I mean in a mathematical way ?我的意思是用数学的方式? The answer is NO .答案是否定的

Now assuming that your data-type for the RoleID column is Integer but role.RoleId is of type/returns value of type String , then there will be a data-type mismatch as one is an integer and the other is a String .现在假设RoleID列的数据类型是Integerrole.RoleId的类型/返回值是String类型,那么就会出现数据类型不匹配,因为一个是integer ,另一个是String

Now, going through the comments , i see that you've solved your issue.Now, let me explain how you solved it.现在,通过评论,我看到你已经解决了你的问题。现在,让我解释你是如何解决它的。

Your sql query looks like this :您的 sql 查询如下所示:

 "SELECT * FROM Roles WHERE RoleID='" & role.roleID & "';"

Let's point out the main relevant part :让我们指出主要的相关部分:

 RoleID='" & role.roleID & "'

Here,before you close the string RoleID= with double quotes, you use ' (single quote).In sql-queries, single quotes are used to declare/give a value(of type STRING ) to the required/given parameter(of the query).在这里,你将关闭字符串之前RoleID=加上双引号,你用' (单引号)。在SQL查询,单引号用于声明/放弃(字符串类型)的值到所需/给定参数(的查询)。

In order to pass an Integer value ,you can pass it without the ' single quote like this :为了传递Integer值,您可以传递它而没有'单引号,如下所示:

 RoleID=12345

So, the answer is simple:You were passing some data of type String to a column which expects data/even if the passed value is of type intger but because you were using ' single quotes when passing the values,the quote had converted it to a String ..So, all u have to do(had to do-in your case) is remove the two single quotes( ' )因此,答案很简单:您将一些String类型的数据传递给需要数据的列/即使传递的值是intger类型但因为您在传递值时使用了'单引号,引号已将其转换为一个String ..所以,你所要做的(在你的情况下必须这样做)就是删除两个单引号( '

Hope this helps to enrich your knowledge :)希望这有助于丰富您的知识:)

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

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