现在我有一段代码,目的是想找出数据库中有没有具体一年(比如说2010年)的数据。如果没有,系统会给出提示:“没有此项记录,无法查询”。代码调试没有问题,可是运行的时候无法显示。请问应该怎么修改或者从新写一下呢?代码如下:
if (a == "jituanzonghe")
{
SqlConnection com = new SqlConnection("");
SqlDataReader dr = null;
try
{
com.Open();
string select_count = "select count(*) from ZH_total where year= '" + b + "'";
SqlDataAdapter sa = new SqlDataAdapter(select_count, com);
DataSet DS = new DataSet();
sa.Fill(DS);
if (Convert.ToInt32(DS.Tables[0].Rows[0][0].ToString()) == 0)
{
Response.Write("<script>alert('没有此项记录,无法查询!');location='view result.aspx'</script>");
}
请高手们给指点一下,谢谢啦~
if (a == "jituanzonghe")
{
SqlConnection com = new SqlConnection("");
SqlDataReader dr = null;
try
{
com.Open();
string select_count = "select count(*) from ZH_total where year= '" + b + "'";
SqlDataAdapter sa = new SqlDataAdapter(select_count, com);
DataSet DS = new DataSet();
sa.Fill(DS);
if (Convert.ToInt32(DS.Tables[0].Rows[0][0].ToString()) == 0)
{
Response.Write("<script>alert('没有此项记录,无法查询!');location='view result.aspx'</script>");
}
请高手们给指点一下,谢谢啦~
是这个意思么?
可以用 DS.Tables[0].rows.Count==0 代替
Convert.ToInt32(DS.Tables[0].Rows[0][0].ToString()) == 0
string select_count = "select count(*) from ZH_total where year= '" + b + "'";
SqlCommand cm=new (select_count,com);
dr=cm.ExecuteReader();
if(dr.read())
{
Response.Write("<script>alert('没有此项记录,无法查询!');location='view result.aspx'</script>");
}
这个试下,看行否代码应该没错,要么数据库里面没有你要的信息当你输入 这个b前后有空格的时候,也可能查询不到结果的
SqlConnection com = new SqlConnection("");
这个里面没有写数据库的连接啊,也就是说没有连接到数据库,怎么有数据呢
select count(*) 必能返回第一行第一列的。返回值必>=0。
SqlDataReader dr = null;
try
{
com.Open();
string select_count = "select * from ZH_total where year= '" + b + "'";
SqlDataAdapter sa = new SqlDataAdapter(select_count, com);
DataSet DS = new DataSet();
sa.Fill(DS);
if (DS.Tables[0].Rows.Count==0)
{
Response.Write("<script>alert('没有此项记录,无法查询!');location='view result.aspx'</script>");
}
这个是修改之后的代码。SQL里改成*了,IF判断里也改成DS.Tables[0].Rows.Count==0了。调试也没有问题,可是就是运行的时候出不来提示的话。。真是很奇怪啊
是不是调试的时候连着测试库,运行连着另外的数据库
Convert.ToInt32(DS.Tables[0].Rows[0][0].ToString()) == 0
UP
LZ的判断是有问题的,你可以采用14楼那哥们的,判断表是否有行,当连行(或者是数据)都没有的话转型是会报错的,是会执行你的catch中的代码的....