C# datagridview 查询代码哪里错了

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 类型,BRemark 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();

    }
sql

2个回答

调试输出下sql,是不是sql拼接错误,还有数据库连接错误。看丢出什么异常。你这代码一贴谁知道

除了,看sql,还看看报错的信息呗!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!