今天项目中遇到的一个问题:判断新闻Id是否存在

 今天做项目中,写了个类,中间用于判断新闻是否存在。其中我的记录只有8条,Id分别为7,8,9,10,11,12,13,14,当用sql语句: private const string SQL_SELECT_COUNT_ID = "select count(1) from [aspnet_Hr] where Id=@Id";的时候,比如:http://www.a.com/default.aspx?id=7就能正常显示,其它的也都能正常显示,提示记录存在,但当Id=10,即http://www.a.com/default.aspx?id=10,明明记录存在,但是提示记录不存在。

当把sql语句换成private const string SQL_SELECT_COUNT_ID = "select count(id) from [aspnet_Hr] where Id=@Id";后,一切都正常了!现在还没太清楚为什么?而在sql server 2005 里运行这两条语句,得到的结果是一样的!

select count(1) from [aspnet_Hr] where Id='10'
select count( Id) from [aspnet_Hr] where Id='10'

namespace ChinaFine.DAL
{

public class Hr:IHr
    {
       Database db = DatabaseFactory.CreateDatabase("WebDataBaseConnectionString");//声明db

       private const string PARM_ID="@Id";

       
 private const string SQL_SELECT_COUNT_ID = "select count(id) from [aspnet_Hr] where Id=@Id";
        //下面这样写为什么当
        private const string SQL_SELECT_COUNT_ID = "select count(id) from [aspnet_Hr] where Id=@Id";

      

        #region IHr 成员

        public bool Exists(int id)
        {
            DbCommand dbCmd = db.GetSqlStringCommand(SQL_SELECT_COUNT_ID);
            db.AddInParameter(dbCmd, PARM_ID, DbType.Int32, id);
            object obj = db.ExecuteScalar(dbCmd);

            int cmdResult;

            if (obj.Equals(null)||obj.Equals(System.DBNull.Value))
            {
                cmdResult=0;
            }
            else
            {
                cmdResult = int.Parse(obj.ToString());
            }

            if (cmdResult>0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }



protected void Page_Load(object sender, EventArgs e)
    {
        ChinaFine.IDAL.IHr news = new ChinaFine.DAL.Hr();

        if (news.Exists(Convert.ToInt32(Request.QueryString["id"])))
        {
            Response.Write("新闻存在");
        }
        else
        {
            Response.Write("新闻不存在");
        }

    }


昏倒,刚才又把项目重新生成一下,用两个都可以了!不知道是怎么回事?

posted @ 2008-06-13 17:32  chinaifne  阅读(606)  评论(1编辑  收藏  举报