以下是学习笔记:
1,常用的查询方法:
例如:根据id查询学生信息
/// <summary> /// 根据ID查询对象 /// </summary> /// <typeparam name="T">查询的对象</typeparam> /// <param name="id">对象ID</param> /// <returns></returns> public Students Find(int id) { //查询语句 string sql = $"SELECT [StudentId] ,[StudentName] ,[Gender] ,[DateOfBirth],[StudentIdNo],[Age],[PhoneNumber],[StudentAddress] ,[ClassId] FROM [dbo].[Students] where StudentId={id}"; //using 使用完会自动释放 using (SqlConnection conn = new SqlConnection(connStsring)) { SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); SqlDataReader dataReader = cmd.ExecuteReader(); Students students = new Students(); if (dataReader.Read()) { students.StudentId = Convert.ToInt32(dataReader["StudentId"]); students.StudentName = dataReader["StudentName"].ToString(); students.Gender = dataReader["Gender"].ToString(); students.DateOfBirth = Convert.ToDateTime(dataReader["DateOfBirth"]); students.StudentIdNo = Convert.ToDecimal(dataReader["StudentIdNo"]); students.ClassId = Convert.ToInt32(dataReader["ClassId"]); students.Age = Convert.ToInt32(dataReader["Age"]); students.PhoneNumber = dataReader["PhoneNumber"].ToString(); students.StudentAddress = dataReader["StudentAddress"].ToString(); } dataReader.Close(); return students; } }
分析上面的代码的问题:
以上只是查询1个学生表,比如还有班级信息表,课程表,商场的商品表,有10个表,100个表,一样的代码要写很多遍
2,以下通过发射来写一个通用查询的方法
/// <summary> /// 根据id查询对象--通用的方法 /// </summary> /// <typeparam name="T">类型的名称</typeparam> /// <param name="id"></param> /// <param name="fieldId">查询的字段</param> /// <returns></returns> public T Find<T>(int id, string fieldName) { //【1】把sql语句写成可以变的 //string sql = $"SELECT [StudentId] ,[StudentName] ,[Gender] ,[DateOfBirth],[StudentIdNo],[Age],[PhoneNumber],[StudentAddress] ,[ClassId] FROM [dbo].[Students] where StudentId={id}";//之前的sql语句是定死的 //先找到类型 Type type = typeof(T); //type.GetProperties() 获取所有的属性 //Select(p => p.Name),linq查询,p => p.Name 属性的名称 //type.Name 表名称 string sql = $"SELECT {string.Join(",", type.GetProperties().Select(p => p.Name))} from {type.Name} where {fieldName}={id}"; using (SqlConnection conn = new SqlConnection(connStsring)) { SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); SqlDataReader dataReader = cmd.ExecuteReader(); //【2】创建的对象类型也要是可变的 //Students students = new Students();//之前的对象这个是定死的 object oObject = Activator.CreateInstance(type); if (dataReader.Read()) { foreach (var prop in type.GetProperties()) { prop.SetValue(oObject, dataReader[prop.Name]); //【3】利用反射来操作属性 } } dataReader.Close(); return (T) oObject; } }
3,使用上面的通用方法
【1】,添加Students类
namespace Models { public class Students { /// <summary> /// 学号 /// </summary> public int StudentId { get; set; } /// <summary> /// 学生姓名 /// </summary> public string StudentName { get; set; } /// <summary> /// 性别 /// </summary> public string Gender { get; set; } /// <summary> /// 生日 /// </summary> public DateTime DateOfBirth { get; set; } /// <summary> /// 考勤号 /// </summary> public decimal StudentIdNo { get; set; } /// <summary> /// 照片 /// </summary> public int ClassId { get; set; } /// <summary> /// 年龄 /// </summary> public int Age { get; set; } /// <summary> /// 电话 /// </summary> public string PhoneNumber { get; set; } /// <summary> /// 地址 /// </summary> public string StudentAddress { get; set; } } }
【2】 调用
MySQLServerHelper mySqlServerHelper=new MySQLServerHelper(); //普通方法 Students students = mySqlServerHelper.Find(100000); //利用反射的通用方法 Students newStudents = mySqlServerHelper.Find<Students>(100000,"StudentId"); Type type11 = typeof(Students); foreach (var prop in type11.GetProperties()) { Console.WriteLine($"{prop.GetValue(newStudents)}"); }