查询为空,大家帮忙看看怎么回事?

alps1624 2008-12-24 11:28:39

protected void Button5_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 3;

string totalQuery = "";//存放所有的查询条件
string showCols = ""; //存放要显示的所有字段
string showbiao = "";
//获取列表框中的所有查询条件
if (ListBox4.Items.Count == 0)
{
Response.Write("<script language='javascript'> alert('请设定查询条件!')</script>");
}
for (int i = 0; i < ListBox4.Items.Count; i++)
{
totalQuery += ListBox4.Items[i].Text + "";

}
//获取复选框列表的所有被选择的字段
if (ListBox6.SelectedIndex > -1)
{
for (int j = 0; j < ListBox6.Items.Count; j++)
{

showCols += ListBox6.Items[j].Text + ",";

}
}
else
{
Response.Write("<script language='javascript'> alert('请选择要显示的字段名称!')</script>");
}
showCols = " " + showCols.TrimEnd(',') + " ";

if (ListBox5.SelectedIndex > -1)
{
for (int j = 0; j < ListBox5.Items.Count; j++)
{

showbiao += ListBox5.Items[j].Text + ",";

}
}
else
{
Response.Write("<script language='javascript'> alert('请选择要显示的表的名称!')</script>");
}
showbiao = " " + showbiao.TrimEnd(',') + " ";



SqlConnection myconn = new SqlConnection("server=localhost;user id=sa;pwd=;DataBase=cbmsXP_db");
string sql = " select " + showCols + " from " + showbiao + " where " + totalQuery + " ";
SqlDataAdapter adapt = new SqlDataAdapter(sql, myconn);
myconn.Open();
DataSet ds = new DataSet();
adapt.Fill(ds);


GridView1.DataSource = ds;
GridView1.DataBind();
Label1.Text = "查询结果:(" + totalQuery + ")";
myconn.Close();
}



ListBox4 ListBox5 ListBox6 都是有数据显示的 就是读取那一块有问题, showbiao showCols totalQuery那为空,我改了几次,还是空值,不知道到底怎么回事?

...全文
101 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
alps1624 2008-12-25
  • 打赏
  • 举报
回复
调试了就是为空,但我不明白sql为啥为空,totalQuery showclos,showbiao为什么赋值不正确??
ljsheng 2008-12-25
  • 打赏
  • 举报
回复
好长!!!!!!!!!
cao_love 2008-12-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zetee 的回复:]
如果你会调试的话,你就知道问题所在,
SqlConnection myconn = new SqlConnection("server=localhost;user id=sa;pwd=;DataBase=cbmsXP_db");
string sql = " select " + showCols + " from " + showbiao + " where " + totalQuery + " "; //断点,得到的SQL 复制出来放到T-SQL里面看是否出错,
SqlDataAdapter adapt = new SqlDataAdapter(sql, myconn);
myconn.Open();

[/Quote]
嗯!同意。
zetee 2008-12-24
  • 打赏
  • 举报
回复
如果你会调试的话,你就知道问题所在,
SqlConnection myconn = new SqlConnection("server=localhost;user id=sa;pwd=;DataBase=cbmsXP_db");
string sql = " select " + showCols + " from " + showbiao + " where " + totalQuery + " "; //断点,得到的SQL 复制出来放到T-SQL里面看是否出错,
SqlDataAdapter adapt = new SqlDataAdapter(sql, myconn);
myconn.Open();
DataSet ds = new DataSet(); //调试看里面的值有没有,
adapt.Fill(ds);

62,269

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧