private void button13_Click(object sender, EventArgs e)
        {
            string sql1 = "(BNum-(select count(*) from bookOut where ";
            string sql = "select BID as 书号,BName as 书名,BWriter as 作者,BPublish as 出版社,BDate as 出版日期,BPrice as 价格," + "BNum as 数量,type as 类型,BRe as 备注, ";
            if (txtBID.Text.Trim() != "")
            {
                sql1 = sql1 + " BID= " + "'" + txtBID.Text.Trim() + "')) as 库存数量";
                sql = sql + sql1 + "from book where BID= " + "'" + txtBID.Text.Trim() + "'";
            }
            else if (txtBName.Text.Trim() != "")
            {
                sql1 = sql1 + "BID=(select BID from book where BName ='" + txtBName.Text + "'))) as 库存数量";
                sql = sql + sql1 + "from book where BName= " + "'" + txtBName.Text + "'";
            }
            else if (txtBWriter.Text.Trim() != "")
            {
                sql1 = sql1 + "BID=(select BID from book where BWriter='" + txtBWriter.Text + "'))) as 库存数量";
                sql = sql + sql1 + "from book where BWriter=" + "'" + txtBWriter.Text + "'";
            }
            else
            {
                MessageBox.Show("清输入正确查询条件", "提醒");
                return;
            }
            conn.Open();
            SqlDataAdapter adp = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            ds.Clear();
            adp.Fill(ds, "book");
            dataGridView4.DataSource = ds.Tables[0].DefaultView;
            conn.Close();
        }

解决方案 »

  1.   

    你把你整个sql语句放到数据库赋值看能不能跑,不能跑报的错误你也能看明白了
      

  2.   

        sql = sql + sql1 + "from book where BID= " + "'" + txtBID.Text.Trim() + "'"; 
    在from前面来个空格
      

  3.   

    在VS 里使用中断,逐步调试找出问题点
    把此SQL 放在DB 里去RUN ,看否是OK
    增加一个IF判断式,看ds.Tables[0] .Rows.Count 是否大于0
      

  4.   

    出现异常时,将鼠标指向sql,或者sql1,仔细看看此时变量的值
      

  5.   

    建议,先在数据库中直接执行Sql,成功后把所有赋值的地方换为参数,在代码中传参执行。拼接如果遇到非法字符时可能返回不确定的错误,例如输入'--