C#教程:读取数据库返回泛型
作者:转载自:xin3721视频教程网更新时间:2010-12-8

 在C#中如果来读取数据库返回泛型,一般是使用ADO.NET查询数据库返回泛型集合,使用SqlDataReader逐行读取数据存入对象。

  代码

  ///

  /// 获取UserInfo泛型集合

  ///

  /// 数据库连接字符串

  /// 要查询的T-SQL

  ///

  public IList GetUserInfoAll(string connStr, string sqlStr)

  {

  using (SqlConnection conn = new SqlConnection(connStr))

  {

  using (SqlCommand cmd = new SqlCommand(sqlStr,conn))

  {

  SqlDataReader sdr = cmd.ExecuteReader();

  IList list = new List();

  while (sdr.Read())

  {

  UserInfo userInfo = new UserInfo();

  userInfo.ID = (Guid) sdr["ID"];

  userInfo.LoginName = sdr["LoginName"].ToString();

  userInfo.LoginPwd = sdr["LoginPwd"].ToString();

  list.Add(userInfo);

  }

  return list;

  }

  }

  }

  这样做虽然返回了需要的数据,但如果当数据库表非常多的时候,针对每一个表都需要去建立这样的一个方法,非常麻烦,也增加了重复劳动力。

  而直接返回DataSet操作起来又不方便,DataSet是一种弱类型。也不如泛型集合操作效率高!

  这个时候就需要我们来提取一个通用的转换方法了?DataSetToList。

  代码

  1  ///

  2         /// 获取泛型集合

  3         ///

  4         /// 类型

  5         /// 数据库连接字符串

  6         /// 要查询的T-SQL

  7         ///

  8          public IList GetList(string connStr, string sqlStr)

  9         {

  10             using (SqlConnection conn = new SqlConnection(connStr))

  11             {

  12                 using (SqlDataAdapter sda = new SqlDataAdapter(sqlStr, conn))

  13                 {

  14                     DataSet ds = new DataSet();

  15                     sda.Fill(ds);

  16                     return DataSetToList(ds, 0);

  17                 }

  18             }

  19         }

代码

  1 ///

  2         /// DataSetToList

  3         ///

  4         /// 转换类型

  5         /// 数据源

  6         /// 需要转换表的索引

  7         /// 泛型集合

  8          public IList DataSetToList(DataSet dataSet, int tableIndex)

  9         {

  10             //确认参数有效

  11              if (dataSet == null || dataSet.Tables.Count <= 0 || tableIndex < 0)

  12                 return null;

  13

  14             DataTable dt = dataSet.Tables[tableIndex];

  15

  16             IList list = new List();

  17

  18

  19             for (int i = 0; i < dt.Rows.Count; i++)

  20             {

  21                 //创建泛型对象

  22                  T _t = Activator.CreateInstance();

  23

  24                 //获取对象所有属性

  25                  PropertyInfo[] propertyInfo = _t.GetType().GetProperties();

  26

  27                 for (int j = 0; j < dt.Columns.Count; j++)

  28                 {

  29                     foreach (PropertyInfo info in propertyInfo)

  30                     {

  31                         //属性名称和列名相同时赋值

  32                         if (dt.Columns[j].ColumnName.ToUpper().Equals(info.Name.ToUpper()))

  33                         {

  34                             if (dt.Rows[i][j]!=DBNull.&#118alue)

  35                             {

  36                                 info.Set&#118alue(_t, dt.Rows[i][j], null);

  37                             }

  38                             else

  39                             {

  40                                 info.Set&#118alue(_t, null, null);

  41                             }

  42

  43                             break;

  44                         }

  45                     }

  46                 }

  47                 list.Add(_t);

  48             }

  49             return list;

  50         }

  使用这种转换方式需要注意的是实体类(model)的属性必须和数据库表的字段名字一致(大小写可以不考虑);

  因为转换时候的匹配是靠属性名称和字段名称匹配的;

  当然如果项目中用到了Linq to Sql 的话就不必这种转换,Linq 已经封装好了;C#应用过程中问题合集正在整理中,后续会编成电子书在论坛供大家下载,请关注。

原文地址https://www.xin3721.com/Article/xin3721_Article_13783.html

关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们     黑ICP备07002182号