日期:2014-05-19  浏览次数:20419 次

object 是什么类型的对象啊?
请问下面的语句:int   programcount   =   Convert.ToInt32(obj);为什么查询的数据为空时返回的programcount   为0;也就是说obj为没有定义的值但是赋值给programcount时为0,为什么呀?


public   void   test()
{
        SqlConnection   conntest   =   new   SqlConnection     (System.Configuration.ConfigurationSettings.AppSettings[ "SqlConn "]);
        SqlCommand   commtest   =   new   SqlCommand();
        string   sql   =   "select   programid   from   programdata   where   programid   =   10777 ";
        object   obj=new   object();
        try
        {
                commtest.CommandType   =CommandType.Text   ;
                commtest.CommandText   =sql;
                commtest.Connection   =   conntest;
                conntest.Open();
                obj   =   commtest.ExecuteScalar();
                obj   =   commtest.ExecuteScalar();
                int   programcount   =   Convert.ToInt32(obj);
                Response.Write(programcount.ToString());
                conntest.Close();
        }
        catch(Exception   e)
      {
              throw   new   Exception(e.Message);
        }
}

------解决方案--------------------
所在对象的基类都是object. 当不清楚一个对象的类型时,可用object来标记它.
------解决方案--------------------
什么叫返回为空啊?只要你的Sql没错

如果没有适合条件的记录,将返回0

当然是0了
------解决方案--------------------
所有对象的基类
------解决方案--------------------
Convert.ToInt32的实现:

public static int ToInt32(object value)
{
if (value != null)
{
return ((IConvertible) value).ToInt32(null);
}
return 0;
}

为null的时候返回0



------解决方案--------------------
传说中的范型~任何类型的基类~其他的类型都是一层一层的继承他下来的~
类型转换的时候~Int32把null转换为0
------解决方案--------------------
obejct基类
------解决方案--------------------
当object类返回的数据给整型赋值时如果为空,返回0,如果返回的数据赋值给字符串时如果为空,返回的为 " ".