未经处理的异常在System.Data.dll中发生。其他信息:在应使用条
机房收费系统中,有些人在联合查询这个模块用的是存储过程,我先尝试着在数据库中建立了一个视图,然后在UI层做个判断并生成查询条件strCondition。 在机房收费系统的联合查询模块中出现的问题:System.Data.SqlClient.SqlException类型的未经处理的异常在 S
机房收费系统中,有些人在联合查询这个模块用的是存储过程,我先尝试着在数据库中建立了一个视图,然后在UI层做个判断并生成查询条件strCondition。
在机房收费系统的“联合查询”模块中出现的问题:“System.Data.SqlClient.SqlException”类型的未经处理的异常在 System.Data.dll 中发生。其他信息: 在应使用条件的上下文(在 '@strCondition' 附近)中指定了非布尔类型的表达式。
出错的DAL层代码为:
Public Function QueryOnLineStatus(ByVal strCondition As String) As List(Of Entity.QueryOnLineStatusViewEntity) Implements IDAL.IQueryOnLineStatusView.QueryOnLineStatus Dim cmdText As String = "select * from QueryOnLineStatus_View where @strCondition" '定义查询字符串(strCondition为UI层传过来的查询条件) Dim cmdType As CommandType = CommandType.Text '定义命令类型 Dim sqlHelper As New SqlHelper '实例化SqlHelper类 Dim myList As List(Of Entity.QueryOnLineStatusViewEntity) Dim dtb As New DataTable Dim parameters As SqlParameter() parameters = {New SqlParameter("@strCondition", strCondition)} dtb = sqlHelper.ExecuteSelect(cmdText, cmdType, parameters) myList = Entity.EntityHelper.ConvertToList(Of Entity.QueryOnLineStatusViewEntity)(dtb) 'EntityHelper.ConvertToList的功能是把DataTable类型转化为泛型集合 Return myList End Function
代码里SQL语句中的strCondition是从UI层传过来的查询条件,此错误发生时,在调试中已经证明查询条件没有错误,如下图:strCondition的值为:“cardNumber='1' ”,所以整个SQL语句不就是“ select * from QueryOnLineStatus_View where cardNumber='1' ”嘛!

最后解决这个问题的办法太出乎我的意料:把原SQL语句"select * from QueryOnLineStatus_View where @strCondition"中“where”和"@strCondition"之间的空格给去掉,将SQL语句变成"select * from QueryOnLineStatus_View where@strCondition"。
经过一阵冥思苦想,在数据库中尝试了好多次后,才明白这到底是为什么。。。。
先看在数据库中测试的结果:
1、当查询语句中where后面是一个值时(假设这个值是1):
(1)、查询语句中where与1之间没有空格,查询出正确结果;
(2)、查询语句中where与1之间有空格,报错:在应使用条件的上下文(在 '1' 附近)中指定了非布尔类型的表达式;
2、假设查询语句中where后面是一条语句时(假设这条语句是“1=1”):
(1)查询语句中where与“1=1”之间有空格,查询出正确结果;
(2)查询语句中where与“1=1”之间没有空格,报错:'=' 附近有语法错误。
原来,SQL语句中,where后面跟的是一个Boolean型的值。
【画龙点睛】
在where后面,如果仅仅是一个Boolean类型的值或者Boolean类型的变量,那么where和这个Boolean值之间是不可以有空格的(这时,我的代码中的@strCondition就是一个Boolean类型的变量;但如果where后面是一条语句的话(当然这一条语句的整体也是一个Boolean类型的值,比如“1=1”),那么这条语句和where之间就必须要有空格。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



1. First, we right-click the blank space of the taskbar and select the [Task Manager] option, or right-click the start logo, and then select the [Task Manager] option. 2. In the opened Task Manager interface, we click the [Services] tab on the far right. 3. In the opened [Service] tab, click the [Open Service] option below. 4. In the [Services] window that opens, right-click the [InternetConnectionSharing(ICS)] service, and then select the [Properties] option. 5. In the properties window that opens, change [Open with] to [Disabled], click [Apply] and then click [OK]. 6. Click the start logo, then click the shutdown button, select [Restart], and complete the computer restart.

Summary of frequently asked questions about importing Excel data into Mysql: How to deal with error log problems encountered when importing data? Importing Excel data into a MySQL database is a common task. However, during this process, we often encounter various errors and problems. One of them is the error log issue. When we try to import data, the system may generate an error log listing the specific information about the error that occurred. So, how should we deal with the error log when we encounter this situation? First, we need to know how

Quickly learn how to open and process CSV format files. With the continuous development of data analysis and processing, CSV format has become one of the widely used file formats. A CSV file is a simple and easy-to-read text file with different data fields separated by commas. Whether in academic research, business analysis or data processing, we often encounter situations where we need to open and process CSV files. The following guide will show you how to quickly learn to open and process CSV format files. Step 1: Understand the CSV file format First,

How to deal with data normalization issues in C++ development. In C++ development, we often need to process various types of data, which often have different value ranges and distribution characteristics. To use this data more efficiently, we often need to normalize it. Data normalization is a data processing technique that maps data of different scales to the same scale range. In this article, we will explore how to deal with data normalization issues in C++ development. The purpose of data normalization is to eliminate the dimensional influence between data and map the data to

In the process of PHP development, dealing with special characters is a common problem, especially in string processing, special characters are often escaped. Among them, converting special characters into single quotes is a relatively common requirement, because in PHP, single quotes are a common way to wrap strings. In this article, we will explain how to handle special character conversion single quotes in PHP and provide specific code examples. In PHP, special characters include but are not limited to single quotes ('), double quotes ("), backslash (), etc. In strings

How to crawl and process data by calling API interface in PHP project? 1. Introduction In PHP projects, we often need to crawl data from other websites and process these data. Many websites provide API interfaces, and we can obtain data by calling these interfaces. This article will introduce how to use PHP to call the API interface to crawl and process data. 2. Obtain the URL and parameters of the API interface. Before starting, we need to obtain the URL of the target API interface and the required parameters.

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

Exception handling and error logging skills in C# Introduction: In the software development process, exception handling and error logging are very important links. For C# developers, mastering exception handling skills and error logging methods can help us better track and debug code, and improve the stability and maintainability of the program. This article will introduce commonly used exception handling techniques in C# and provide specific code examples to help readers better understand and apply exception handling and error logging. 1. Basic concepts of exception handling Exceptions refer to the
