This article is a mirror article of machine translation, please click here to jump to the original article.

View: 41320|Reply: 12

[ASP.NET] [DBHelp] uses reflection technology to encapsulate objects into Lists

[Copy link]
Posted on 4/20/2015 2:52:57 PM | | | |
This post was last edited by Delver_Si on 2015-4-21 16:09

What type of entity is given to me, and what type of entity collection is returned

  1. //得到一个list<T>
  2.         public static List<T> getList<T>(string strSql)
  3.         {
  4.             List<T> list = new List<T>();

  5.             SqlConnection sqlConn = new SqlConnection(strConn);
  6.             SqlCommand sqlComm = new SqlCommand(strSql, sqlConn);
  7.             sqlConn.Open();
  8.             SqlDataReader sqlDR = sqlComm.ExecuteReader();

  9.             while (sqlDR.Read())
  10.             {
  11.                 //得到T的类型
  12.                 Type t = typeof(T); ;

  13.                 //查看类中的属性:
  14.                 PropertyInfo[] pis = t.GetProperties();
  15.                 // 用反射生成对象
  16.                 T model = Activator.CreateInstance<T>();
  17.                 foreach (PropertyInfo pi in pis)
  18.                 {
  19.                     if (pi!= null)
  20.                     {
  21.                         //取得特定字段并赋值
  22.                         pi.SetValue(model, sqlDR[pi.Name].ToString(), null);
  23.                     }
  24.                     }

  25.                 list.Add(model);
  26.             }
  27.             sqlConn.Close();

  28.             return list;

  29.         }
Copy code

The whole DBHelp.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Reflection;

  8. namespace ManualAssign
  9. {
  10.     public class DBHelp
  11.     {
  12.         //数据库连接字符串
  13.         private static string strConn = "server=.;database=ManualAssign;integrated security=true";

  14.         //得到一个DataTable
  15.         public static DataTable getTable(string strSql)
  16.         {
  17.             SqlConnection sqlConn = new SqlConnection(strConn);
  18.             SqlDataAdapter sqlDA = new SqlDataAdapter(strSql,sqlConn);
  19.             DataTable dt = new DataTable();
  20.             sqlDA.Fill(dt);
  21.             return dt;
  22.         }

  23.         //增删改
  24.         public static void upDateDB(string strSql)
  25.         {
  26.             SqlConnection sqlConn = new SqlConnection(strConn);
  27.             SqlCommand sqlComm = new SqlCommand(strSql, sqlConn);
  28.             sqlConn.Open();
  29.             sqlComm.ExecuteNonQuery();
  30.             sqlConn.Close();
  31.         }


  32.         //得到一个list<T>
  33.         public static List<T> getList<T>(string strSql)
  34.         {
  35.             List<T> list = new List<T>();

  36.             SqlConnection sqlConn = new SqlConnection(strConn);
  37.             SqlCommand sqlComm = new SqlCommand(strSql, sqlConn);
  38.             sqlConn.Open();
  39.             SqlDataReader sqlDR = sqlComm.ExecuteReader();

  40.             while (sqlDR.Read())
  41.             {
  42.                 //得到T的类型
  43.                 Type t = typeof(T); ;

  44.                 //查看类中的属性:
  45.                 PropertyInfo[] pis = t.GetProperties();
  46.                 // 用反射生成对象
  47.                 T model = Activator.CreateInstance<T>();
  48.                 foreach (PropertyInfo pi in pis)
  49.                 {
  50.                     if (pi!= null)
  51.                     {
  52.                         //取得特定字段并赋值
  53.                         pi.SetValue(model, sqlDR[pi.Name].ToString(), null);
  54.                     }
  55.                     }

  56.                 list.Add(model);
  57.             }
  58.             sqlConn.Close();

  59.             return list;

  60.         }

  61.     }
  62. }
Copy code



Score

Number of participants1MB+1 contribute+1 Collapse reason
Little scum + 1 + 1 Very powerful!

See all ratings





Previous:Ruijie school network is unstable and often drops, I have a solution!
Next:Japan develops ultra-high-speed cameras
 Landlord| Posted on 4/20/2015 3:56:40 PM |
Xiao Zhazha Posted on 2015-4-20 15:36
I'll try this at night, it would be better if there was an example

There are examples
 Landlord| Posted on 4/20/2015 3:56:38 PM |
Xiao Zhazha Posted on 2015-4-20 15:36
I'll try this at night, it would be better if there was an example

There are examples
Posted on 8/1/2015 3:57:53 PM |
Attached is a DBHelp transaction execution code

Posted on 4/20/2015 3:36:14 PM |
I'll try this at night, it would be better if there was an example
 Landlord| Posted on 4/21/2015 4:10:01 PM |
updated
Posted on 4/21/2015 4:50:50 PM |
Can you upload an example of a compressed package?
 Landlord| Posted on 4/21/2015 9:06:48 PM |
source

ManualAssign.rar

180.66 KB, Download times: 6, Download credits: -1 prestige, -1 contribution

 Landlord| Posted on 4/21/2015 9:07:00 PM |
Xiao Zhazha Posted on 2015-4-21 16:50
Can you upload an example of a compressed package?

sent
Posted on 4/22/2015 8:31:39 AM |

Okay, I'll take a look
Posted on 4/22/2015 10:51:19 AM |

Take it away, and say thank you
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com