解释都在注释里了,会不断更新
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { public class Student { public int Id { get; set; } public string Name { get; set; } } }
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { public class DBHelper<T> where T : class, new() { //获取数据库连接名称(在appsettings.json配置文件下配置) private static string con = "Data Source=.;Initial Catalog=hahha;user=sa;password=010806wpz.;"; /// <summary> /// 查询 返回list泛型集合 /// </summary> /// <param name="where"></param> /// <returns></returns> public List<T> Query(string where) { DataTable tb = new DataTable(); List<T> list = new List<T>(); string sql = GetQuerySql(); sql += where; //将连接字符串赋值,获取到一个新实例 using (SqlConnection connection = new SqlConnection(con)) { //打开数据库的连接 connection.Open(); //实例化sqlcommand using (SqlCommand command = new SqlCommand(sql, connection)) { SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(command); sqlDataAdapter.Fill(tb); //获取T的类型 Type type = typeof(T); //循环行 for (int i = 0; i < tb.Rows.Count; i++) { //实例化T,每一次都需要实例化new 对象 Object obj = Activator.CreateInstance(type); for (int j = 0; j < tb.Columns.Count; j++) { //获取列的名称及类型 //tb.Columns[j].ColumnName代表每一列字段的名称(例如:ID,Name) //type.GetProperty:通过字段名称返回一个对象 //入参:要获取的属性名的字符串 //返回值(出参):返回符合指定要求的属性的对象(包括:字段类型,字段名称等) PropertyInfo info = type.GetProperty(tb.Columns[j].ColumnName);//赋值 #region 类型的判断并赋值 //int类型 if (tb.Columns[j].DataType == typeof(Int32)) { //有没有可能空值? if (tb.Rows[i][j] != null) { //obj.setValue(info,12); info.SetValue(obj, int.Parse(tb.Rows[i][j].ToString()), null); } else { //null值的情况 info.SetValue(obj, 0, null); } } //float类型 else if (tb.Columns[j].DataType == typeof(float)) { //有没有可能空值? if (tb.Rows[i][j] != null) { info.SetValue(obj, float.Parse(tb.Rows[i][j].ToString()), null); } else { //null值的情况 info.SetValue(obj, 0.0f, null); } } //datetime else if (tb.Columns[j].DataType == typeof(DateTime)) { //有没有可能空值? if (tb.Rows[i][j] != null) { info.SetValue(obj, DateTime.Parse(tb.Rows[i][j].ToString()), null); } else { //null值的情况 info.SetValue(obj, DateTime.Now, null); } } //double else if (tb.Columns[j].DataType == typeof(double)) { //有没有可能空值? if (tb.Rows[i][j] != null) { info.SetValue(obj, double.Parse(tb.Rows[i][j].ToString()), null); } else { //null值的情况 info.SetValue(obj, 0.00, null); } } //GUID else if (tb.Columns[j].DataType == typeof(Guid)) { //有没有可能空值? if (tb.Rows[i][j] != null && !tb.Rows[i][j].ToString().Equals("")) { info.SetValue(obj, Guid.Parse(tb.Rows[i][j].ToString()), null); } else { //null值的情况 info.SetValue(obj, Guid.Parse("00000000-0000-0000-0000-000000000000"), null); } } else { //string //有没有可能空值? if (tb.Rows[i][j] != null) { info.SetValue(obj, tb.Rows[i][j].ToString(), null); } else { //null值的情况 info.SetValue(obj, "", null); } } #endregion } //将object 类型强转对应的类型 list.Add((T)obj);//(类型)强制转换 } } } return list; } //获取sql public string GetQuerySql() { Type type = typeof(T); //type.Name获取类的名称 //无需实例化 string sql = ""; sql = "select * from " + type.Name + " where 1=1 "; return sql; } } }
using System; using System.Collections.Generic; namespace ConsoleApp1 { class Program { static void Main(string[] args) { DBHelper<Student> dBHelper = new DBHelper<Student>(); List<Student> list= dBHelper.Query(""); Console.WriteLine("数据量为:"+list.Count); Console.ReadKey(); } } }
目前只写了查询,增删改会再后续更新