• 不使用EF框架实现数据库增删改查


    创建一个通用的数据访问层 (DAL) 类,让所有需要操作数据库的 DAL 类都继承这个基类,以实现通用的增删改查方法。下面是一个简单的示例展示如何实现这样的通用基类:

    1. public class BaseDal<T> where T : class
    2. {
    3. protected string connectionString; // 数据库连接字符串
    4. public BaseDal(string connectionString)
    5. {
    6. this.connectionString = connectionString;
    7. }
    8. public void Insert(T entity)
    9. {
    10. string tableName = typeof(T).Name; // 获取表名
    11. string insertQuery = $"INSERT INTO {tableName} ({GetColumns()}) VALUES ({GetParams()})";
    12. using (SqlConnection connection = new SqlConnection(connectionString))
    13. {
    14. connection.Open();
    15. using (SqlCommand command = new SqlCommand(insertQuery, connection))
    16. {
    17. SetParameters(command, entity);
    18. command.ExecuteNonQuery();
    19. }
    20. }
    21. }
    22. public void Update(T entity)
    23. {
    24. string tableName = typeof(T).Name; // 获取表名
    25. string updateQuery = $"UPDATE {tableName} SET {GetUpdateParams()} WHERE Id = @Id";
    26. using (SqlConnection connection = new SqlConnection(connectionString))
    27. {
    28. connection.Open();
    29. using (SqlCommand command = new SqlCommand(updateQuery, connection))
    30. {
    31. SetParameters(command, entity);
    32. command.ExecuteNonQuery();
    33. }
    34. }
    35. }
    36. public void Delete(int id)
    37. {
    38. string tableName = typeof(T).Name; // 获取表名
    39. string deleteQuery = $"DELETE FROM {tableName} WHERE Id = @Id";
    40. using (SqlConnection connection = new SqlConnection(connectionString))
    41. {
    42. connection.Open();
    43. using (SqlCommand command = new SqlCommand(deleteQuery, connection))
    44. {
    45. command.Parameters.AddWithValue("@Id", id);
    46. command.ExecuteNonQuery();
    47. }
    48. }
    49. }
    50. // 获取实体类的所有属性名
    51. private string GetColumns()
    52. {
    53. var properties = typeof(T).GetProperties().Where(p => p.Name != "Id"); // 排除 Id 属性
    54. return string.Join(", ", properties.Select(p => p.Name));
    55. }
    56. // 获取参数列表
    57. private string GetParams()
    58. {
    59. var properties = typeof(T).GetProperties().Where(p => p.Name != "Id"); // 排除 Id 属性
    60. return string.Join(", ", properties.Select(p => "@" + p.Name));
    61. }
    62. // 获取更新参数列表
    63. private string GetUpdateParams()
    64. {
    65. var properties = typeof(T).GetProperties().Where(p => p.Name != "Id"); // 排除 Id 属性
    66. return string.Join(", ", properties.Select(p => $"{p.Name} = @{p.Name}"));
    67. }
    68. // 设置参数
    69. private void SetParameters(SqlCommand command, T entity)
    70. {
    71. var properties = typeof(T).GetProperties().Where(p => p.Name != "Id"); // 排除 Id 属性
    72. foreach (var property in properties)
    73. {
    74. command.Parameters.AddWithValue("@" + property.Name, property.GetValue(entity));
    75. }
    76. }
    77. }

    在这个示例中,BaseDal 是一个泛型基类,其中包含了通用的增删改方法。在这个基类中,我们使用了反射来获取实体类的属性,并根据属性动态生成 SQL 语句和设置参数。其他需要操作数据库的 DAL 类可以继承这个基类,从而实现通用的增删改查操作。

  • 相关阅读:
    【深度学习 AIGC 绘画】Robert001/UniControl-Demo docker
    Vue内置组件之KeepAlive原理
    【SLAM-建图】Ubuntu18.04安装cartographer记录
    赫夫曼树
    苹果Mac电脑fcpx视频剪辑:Final Cut Pro中文最新 for mac
    【MATLAB第75期】#源码分享 | 基于MATLAB的不规则间隔数据插值实现时间序列数据扩充(更新中)
    【Echarts多种曲线实例】Echarts多条曲线不同时间轴对比(附代码)
    算法入门01二维数组中的查找
    新能源国标接入随想
    JAVA毕业设计客户关系智能管理系统计算机源码+lw文档+系统+调试部署+数据库
  • 原文地址:https://blog.csdn.net/weixin_50784508/article/details/138568924