日期:2014-05-18  浏览次数:20431 次

如果数据库中没数据,就提示“没查到”代码该怎么写?
如果数据库中没数据,就提示“没查到”代码该怎么写?用这段代码改.急用!谢谢
我就这么多分了

<%@   Page   %>
<%@   Import   namespace= "Discuz.Forum "   %>
<%@   Import   namespace= "Discuz.Common "   %>
<%@   Import   namespace= "System.Web.UI.WebControls "   %>
<%@   Import   namespace= "System.Data "   %>
<script   runat= "server ">
override   protected   void   OnInit(EventArgs   e)
{
string   timeb7=Utils.GetDateTime(-7);//7天前
Response.Write( "士大夫 <BR> ");

try
{
DataTable   dt;
string   TextSql=String.Format( "select   top   10   username   ,extcredits2     from   dnt_users   order   by   extcredits2   desc ");
    dt   =   Database.ExecuteDataset(System.Data.CommandType.Text,   TextSql).Tables[0];
foreach   (DataRow   dr   in   dt.Rows)
{
Response.Write(dr[0].ToString().Trim());
Response.Write( "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ");
Response.Write(dr[1].ToString().Trim());
Response.Write( " <br> ");
}
}
catch
{
Response.Write( "document.write( '参数错误,请检查! '); ");
}
finally
{
Response.End();
}
base.OnInit(e);
}

</script>


------解决方案--------------------
if(dt.Rows.Count == 0)
{
Response.Write( "没查到 ");
}
else
{
foreach (DataRow dr in dt.Rows)
{
//.........
}
}
------解决方案--------------------
如果是使用 GridView 显示数据,直接设置其EmptyData 属性为“没查到”。
------解决方案--------------------
override protected void OnInit(EventArgs e)
{
string timeb7=Utils.GetDateTime(-7);//7天前
Response.Write( "士大夫 <BR> ");

try
{
DataTable dt;
string TextSql=String.Format( "select top 10 username ,extcredits2 from dnt_users order by extcredits2 desc ");
dt = Database.ExecuteDataset(System.Data.CommandType.Text, TextSql).Tables[0];
/***************************添加代码*************************
if(dt == null || dt.Rows.Count==0)
{
Response.Write( "document.write( '数据库空! '); ");
return;
}
/**************************************************************
foreach (DataRow dr in dt.Rows)
{
Response.Write(dr[0].ToString().Trim());
Response.Write( "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ");
Response.Write(dr[1].ToString().Trim());
Response.Write( " <br> ");
}
}
catch
{
Response.Write( "document.write( '参数错误,请检查! '); ");
}
finally
{
Response.End();
}
base.OnInit(e);
}

</script>