未经处理的异常在 System.Data.dll 中发生。其他信息:在应使用条件的上下文(在 '***' 附近)中指定了非布尔类型的表达式。

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/huyuyang6688/article/details/38322005

           

         机房收费系统中,有些人在联合查询这个模块用的是存储过程,我先尝试着在数据库中建立了一个视图,然后在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之间就必须要有空格。

         

         

展开阅读全文

"System.Data.SqlClient.SqlException"类型的异常在 System.Data.dll发生,

12-22

“System.Data.SqlClient.SqlException”类型的异常在 System.Data.dll 中发生,但未在用户代码中进行处理rnrn其他信息: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - 无法打开到 SQL Server 的连接)rnrn说是sql没有连接上,但是sql能正常能打开,也允许了远程连接,小白求大神指教rn代码如下rnusing System;rnusing System.Data;rnusing System.Data.SqlClient;rnusing System.Configuration;rnusing System.Collections.Generic;rnusing System.Linq;rnusing System.Web;rnusing System.Web.Security;rnusing System.Web.UI.HtmlControls;rnusing System.Web.UI;rnusing System.Web.UI.WebControls;rnusing System.Web.UI.WebControls.WebParts;rnrnpublic partial class _Default : System.Web.UI.Pagernrn protected void Page_Load(object sender, EventArgs e)rn rnrn rn protected void BTQuery_Click(object sender, EventArgs e)rn rn string connString;rn string SName, CName;rn //获取姓名和课程名rn SName = TBSname.Text;rn CName = TBCname.Text;rn //设置数据库连接串,使用系统认证rn connString = "Initial Catalog=jiaoxuedb;Data Source=mxm;Integrated Security=SSPI;";rn SqlConnection Conn = new SqlConnection(connString);rn SqlCommand QueryCommand = new SqlCommand("SELECT score FROM Student,Course,SC" +rn "Where Student.Sno=SC.Sno and Course.Cno=SC.Cno and " +rn "Sname=@SName and Cname=@CName",Conn);rn //Add the parameters for the SelectCommandrn QueryCommand.Parameters.Add("@SName", SqlDbType.Char, 8);rn QueryCommand.Parameters.Add("@Cname", SqlDbType.Char, 20);rn QueryCommand.Parameters["@SName"].Value = SName;rn QueryCommand.Parameters["@CName"].Value = CName;rn Conn.Open();rn //执行 QueryCommand.ExecuteScalar方法查询成绩rn tryrn rn Int64 ScoreValue = (Int64)QueryCommand.ExecuteScalar();rn LaScore.Text = Convert.ToString(ScoreValue);rn rn catch//错误处理rn rn LaScore.Text = "没有成绩";rn rn finallyrn rn Conn.Close();rn rn rn 论坛

“System.Data.OleDb.OleDbException”类型未经处理的异常在 System.Data.dll发生 其他信息: 找不到文件

12-07

using System;rnusing System.Collections.Generic;rnusing System.ComponentModel;rnusing System.Data;rnusing System.Drawing;rnusing System.Linq;rnusing System.Text;rnusing System.Threading.Tasks;rnusing System.Windows.Forms;rnusing System.IO;rnusing System.Data.OleDb;rnrnnamespace yy加密rnrn public partial class Form5 : Formrn rn static public string userid;//用户名,用于保存rn static public string password;//密码,用于保存rn public Form5()rn rn InitializeComponent();rn rn private bool pdyj()rn rn if (textBox1.Text == "")rn return false;rn if (textBox2.Text == "")rn return false;rn return true;rn rn private void button1_Click(object sender, EventArgs e)rn rn if (!pdyj())rn rn MessageBox.Show("请输入正确信息");rn return;rn rn string conStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\houtai.accdb;";rn OleDbConnection con = new OleDbConnection(conStr);rn con.Open();rn string cmdStr = "insert into [houtai] ([userid],[password]) values('"rn + textBox1.Text + "','" + textBox2.Text + "')"; //构造sql语句rn OleDbCommand cmd = new OleDbCommand(cmdStr, con); //定义Command对象rn cmd.ExecuteNonQuery(); //执行Command命令rn con.Close(); //关闭数据库连接rn MessageBox.Show("注册成功!");rn Form1 zy = new Form1();rn zy.ShowDialog();rn this.Hide();rn this.Dispose();rnrn rnrn private void button2_Click(object sender, EventArgs e)rn rn Form1 ii = new Form1();rn ii.Show();rn this.Hide();rn rn rnrn小白求解,一直提示找不到houtai.accdb文件, 论坛

在应使用条件上下文(在'|1|'附近)中指定了非布尔类型表达式!求高手进来解答!!!

08-18

DAL层:rnpublic Users SelectUserByUserNameAndPassWordAndPowerId(string userName, string passWord,string powerId) rn rn Users users = null;rn string sql = "select * from Users where UserName=" + "'" + userName + "' and PassWord=" + "'" + passWord + "'and " + "'"+powerId+"'";rn using (SqlDataReader reader = DBHelper.ExcuteReader(sql))rn rn if (reader.Read()) rn rn users = new Users();rn users.Id = Convert.ToInt32(reader["Id"]);rn users.UserName = reader["UserName"].ToString();rn users.Password = reader["PassWord"].ToString();rn users.PowerId = Convert.ToInt32(reader["PowerId"]);rn users.Lastlogip = reader["LastLogIp"].ToString();rn users.Logtime = Convert.ToDateTime(reader["LogTime"]);rn users.Isloged = Convert.ToInt32(reader["IsLoged"]);rn rn return users;rn rn rnBLL层:rn public static bool Login(string UserName,string PassWord,string PowerId) rn rn UsersDAL usersdal = new UsersDAL();rn Users user = null;rn user = usersdal.SelectUserByUserNameAndPassWordAndPowerId(UserName, PassWord, PowerId);rn if (user==null)rn rn return false;rn rn else rn rn return true ;rn rn rnWEB层:rn rn 账 号rn rn rn rn rn rn rn rn rn 密 码rn rn rn rn rn rn rn rn rn rn rn 学工处rn 宿舍楼管rn 维修人员 rn rn rn rn rn rn 学生请点击此处 rn rn rn rn rn rn rn rn rn对应的cs代码:rnprotected void RoleDropDownList_SelectedIndexChanged()rn rn if (RoleDropDownList.SelectedIndex == 1) rn rn Response.Redirect("Admin/Default.aspx");rn rn if (RoleDropDownList.SelectedIndex == 2)rn rn Response.Redirect("Department/Default.aspx");rn rn if (RoleDropDownList.SelectedIndex == 3)rn rn Response.Redirect("Maintain/Default.aspx");rn rn rn protected void txtButton_Click(object sender, EventArgs e) rn rn string UserName = txtUserName.Text.Trim();rn string PassWord = txtUserPwd.Text.Trim();rn string PowerId = RoleDropDownList.Text.Trim();rn bool success = UsersBLL.Login(UserName, PassWord,PowerId);rn if (success)rn rn RoleDropDownList_SelectedIndexChanged();rn rn else rn rn lblmessage.Text = "用户名或密码出错!";rn rn rn急这解决,求高手进来解答吧!!! 论坛

在应使用条件上下文(在 '00001' 附近)中指定了非布尔类型表达式

11-11

-- 存储过程的主要目的是得到库存里没有的商品信息rncreate Proc [dbo].[warenotinmanager] rn@WareClause Varchar(2000), --商品信息条件rn@Depart Varchar(1000), --部门信息条件rn@begindate char(8), --开始时间rn@Enddate char(8) --结束时间rnAs rn rncreate Table #Ware(warecode char(10),trademarkcode char(10),specification char(20),warename varchar(50)) --建立临时表,存储商品信息的编码,品牌编码,款号,商品的名称rncreate table #dept(deptcode char(10)) --建立临时表,存储部门的编码rn rnIF Isnull( @wareclause ,'') <> '' rnSet @wareclause = ' Where ' + @wareClause rnExec( 'Insert #Ware(warecode,trademarkcode,specification,warename )rn Select warecode ,trademarkcode,specification,warename From sawarecode ' + @wareclause ) rnrnIF Isnull( @Depart ,'') <> '' rnSet @Depart = ' Where ' + @Depart rnexec('insert #dept(deptcode) select DepartmentCode from stdepartment'+@depart)rnrnselect saTrademark.TrademarkName,specification,warename from #Ware ,saTrademarkrnwhere #ware.trademarkcode=saTrademark.TrademarkCode and warecode not inrn(select warecode from whStockManage where WareHouseID in (select deptcode from #dept) )rn--saTrademark为品牌表 whStockManage 为库存表rn rnrnexec warenotinmanager '00001','J001','',''rn消息 4145,级别 15,状态 1,第 2 行rn在应使用条件的上下文(在 '00001' 附近)中指定了非布尔类型的表达式。rn消息 4145,级别 15,状态 1,第 1 行rn在应使用条件的上下文(在 'J001' 附近)中指定了非布尔类型的表达式。rnrn(0 行受影响)rnrnrn请各位告诉我一下那里错了?rnexec warenotinmanager('','','','')执行这个就没有问题,请问一下怎么改rn 论坛

“System.InvalidCastException”类型未经处理的异常在 MyContact.exe 中发生 其他信息: 指定的转换无效。

12-23

在做通讯录界面,实在是不知道出了什么问题rn登录窗体和闪屏窗体都可以实现,就是主界面出了这个问题rnrn我是新手,各位高手帮帮忙啦rn这是主窗体的程序rnusing System;rn  using System.Collections.Generic;rn  using System.ComponentModel;rn  using System.Data;rn  using System.Drawing;rn  using System.Text;rn  using System.Windows.Forms;rn  using MyContact.DAL;rnusing Aptech.UI;rnnamespace MyContactrnrn public partial class FrmMain : MyContact.FrmBasern rn private FrmCard frmCard = null;rnrn public FrmMain()rn rn InitializeComponent();rn rnrn public User CurrentUser = null;rn   rn private void FrmMain_Load(object sender, EventArgs e)rn rn if (CurrentUser != null)rn rn picPhoto.Image = CurrentUser.Sex == "男" ? imgSex.Images["boy"] : imgSex.Images["girl"];rn this.lblAccount.Text = CurrentUser.NickName;rn this.lblInfo.Text = string.Format("共有 0 位好友", RoseManager.GetAllRoseCount());rn initSbGroup();rn rn rn rnrn //加载分组后的联系人列表rn   /// rn   /// 加载分组后的联系人列表rn   /// rn /// rn   private void initSbGroup()rn   rn   sbGroup.Groups.Clear();rn   DataTable groups = GroupManager.GetAllGroups();rn   if (groups == null || groups.Rows.Count == 0) return;rn   for (int i = 0; i < groups.Rows.Count; i++)rn   rn   DataRow r = groups.Rows[i];rn   int groupId = (int)r["groupId"];rn   string groupName = r["groupName"].ToString();rn   SbGroup group = new SbGroup(groupName);rn   group.Tag = groupId;rn initSbItem(group, groupId);rn   sbGroup.Groups.Add(group);rn rn    sbGroup.VisibleGroupIndex = 0;rn   rn   private void initSbItem(SbGroup group, int groupId)rn   rn   DataTable dtRose = RoseManager.GetRoseByGroupId(groupId);rn   if (dtRose == null || dtRose.Rows.Count == 0) return;rn   for (int i = 0; i < dtRose.Rows.Count; i++)rn   rn   DataRow r = dtRose.Rows[i];rn    rn   string nickName = r["nickName"].ToString();rn   SbItem sbItem = new SbItem(nickName, 0);rn  rn   //sbItem.ContextMenuStrip = this.contextMenuStrip1;rnrn sbItem.Tag = this.ConvertRowToRose(r); rn   group.Items.Add(sbItem);rn   rn   rn private Rose ConvertRowToRose(DataRow r)rn rn Rose rose = new Rose();rn rose[/color].Id = (int)r["Id"];rn rose.NickName = r["nickName"].ToString();rn rose.RealName = r["realName"].ToString();rn rose.Sex =bool.Parse( r["sex"].ToString());rn rose.Mobile = r["mobile"].ToString();rn rose.Tel = r["tel"].ToString();rn rose.Email = r["email"].ToString();rn rose.QQ = r["qq"].ToString();rn rose.BirthDay = DateTime.Parse(r["birthday"].ToString());rn rose.Addr = r["Addr"].ToString();rn rose.Note = r["Note"].ToString();rnrn rose.StarId = (int)r["starId"];rn rose.BloodId =(int)r["bloodId"];rn rose.GroupId = (int)r["groupId"];rnrn return rose;rn rn rnrn private void sbGroup_MouseMove(object sender, MouseEventArgs e)rn rn if (frmCard == null || frmCard.IsDisposed) frmCard = new FrmCard();rn rn rn SbComponent obj = sbGroup.GetComponentAt(e.X, e.Y);rn if (obj != null)rn rn if (obj.Tag != null)rn rn Rose rose = obj.Tag as Rose;rn if (rose != null)rn rn //TODO:给FrmCard窗体相关属性赋值,并显示该窗体rn frmCard.Rose = rose;rn frmCard.Photo = rose.Sex ? this.imgSex.Images["boy"] : this.imgSex.Images["girl"];rn frmCard.Left = this.Left + this.Width;rn frmCard.Top = this.Top;rn frmCard.Show();rn rn elsern rn if (frmCard.Visible) frmCard.Hide();rn rn rn elsern rn if (frmCard.Visible) frmCard.Hide();rn rn rn elsern rn if (frmCard.Visible) frmCard.Hide();rn rn rn rn 论坛

没有更多推荐了,返回首页