In C#, what is the best way to test if a dataset is empty?

后端 未结 6 776
無奈伤痛
無奈伤痛 2021-01-12 13:59

I know you can look at the row.count or tables.count, but are there other ways to tell if a dataset is empty?

6条回答
  •  悲哀的现实
    2021-01-12 14:27

    I have created a small static util class just for that purpose

    Below code should read like an English sentence.

        public static bool DataSetIsEmpty(DataSet ds)
        {
            return !DataTableExists(ds) && !DataRowExists(ds.Tables[0].Rows);
        }
    
        public static bool DataTableExists(DataSet ds)
        {
            return ds.Tables != null && ds.Tables.Count > 0;
        }
    
        public static bool DataRowExists(DataRowCollection rows)
        {
            return rows != null && rows.Count > 0;
        }
    

    I would just put something like below code and be done with it. Writing a readable code does count.

            if (DataAccessUtil.DataSetIsEmpty(ds)) {
                return null;
            }
    

提交回复
热议问题