• 不使用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 类可以继承这个基类,从而实现通用的增删改查操作。

  • 相关阅读:
    文举论金:黄金原油全面走势分析
    算法题:平均数为k的最长连续子数组
    postgresql分区表
    ssh key与git/github生成密钥
    web前端tips:js继承——寄生式继承
    计算机毕业设计 SSM学校图书借阅管理系统 图书馆借阅管理系统 图书借阅系统Java
    猿创征文 | 专做药品生产研发的程序员
    C# Winform编程(4)多文档窗口(MDI)
    LAXCUS分布式操作系统相比LINUX的优势
    记录获取蓝鲸智云token的过程
  • 原文地址:https://blog.csdn.net/weixin_50784508/article/details/138568924