conn.Open();
comm.Connection=conn;
comm.CommandType=CommandType.StoredProcedure;
comm.Parameters.Clear();
comm.CommandText="MemberQueryLoginName";
comm.Parameters.Add(new SqlParameter("@LoginName","webmaster"));
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=comm;
DataTable dt=new DataTable();
da.Fill(dt);if (dt==null)            //  <-----这么写,不管能不能查找到记录,他最后都“不为空”
this.Label1.Text="为空";
else
this.Label1.Text="不为空"如果换成下面的,就好了,DataTable 不能直接来判断null 吗?还是我的格式写得有问题?
if (dt.Rows.Count==0)            
this.Label1.Text="为空";
else
this.Label1.Text="不为空"
还有我用ExecuteNonQuery()返回Select 语句的存储过程,他都是-1,那我怎样判断是否找到了记录?

解决方案 »

  1.   

    你已经执行了DataTable dt=new DataTable();
    dt就已经不是null了
    如果是null说明不存在datatable的实例
    dt.Rows.Count==0说明已经创建了datatable实例,但是datatable中没有数据MSDN对ExecuteNonQuery()的解释:
    For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.