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

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

public class BaseDal<T> where T : class
{
    protected string connectionString; // 数据库连接字符串

    public BaseDal(string connectionString)
    {
        this.connectionString = connectionString;
    }

    public void Insert(T entity)
    {
        string tableName = typeof(T).Name; // 获取表名
        string insertQuery = $"INSERT INTO {tableName} ({GetColumns()}) VALUES ({GetParams()})";

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using (SqlCommand command = new SqlCommand(insertQuery, connection))
            {
                SetParameters(command, entity);
                command.ExecuteNonQuery();
            }
        }
    }

    public void Update(T entity)
    {
        string tableName = typeof(T).Name; // 获取表名
        string updateQuery = $"UPDATE {tableName} SET {GetUpdateParams()} WHERE Id = @Id";

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using (SqlCommand command = new SqlCommand(updateQuery, connection))
            {
                SetParameters(command, entity);
                command.ExecuteNonQuery();
            }
        }
    }

    public void Delete(int id)
    {
        string tableName = typeof(T).Name; // 获取表名
        string deleteQuery = $"DELETE FROM {tableName} WHERE Id = @Id";

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            using (SqlCommand command = new SqlCommand(deleteQuery, connection))
            {
                command.Parameters.AddWithValue("@Id", id);
                command.ExecuteNonQuery();
            }
        }
    }

    // 获取实体类的所有属性名
    private string GetColumns()
    {
        var properties = typeof(T).GetProperties().Where(p => p.Name != "Id"); // 排除 Id 属性
        return string.Join(", ", properties.Select(p => p.Name));
    }

    // 获取参数列表
    private string GetParams()
    {
        var properties = typeof(T).GetProperties().Where(p => p.Name != "Id"); // 排除 Id 属性
        return string.Join(", ", properties.Select(p => "@" + p.Name));
    }

    // 获取更新参数列表
    private string GetUpdateParams()
    {
        var properties = typeof(T).GetProperties().Where(p => p.Name != "Id"); // 排除 Id 属性
        return string.Join(", ", properties.Select(p => $"{p.Name} = @{p.Name}"));
    }

    // 设置参数
    private void SetParameters(SqlCommand command, T entity)
    {
        var properties = typeof(T).GetProperties().Where(p => p.Name != "Id"); // 排除 Id 属性
        foreach (var property in properties)
        {
            command.Parameters.AddWithValue("@" + property.Name, property.GetValue(entity));
        }
    }
}

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

相关推荐

  1. 使用EF框架实现数据库增删

    2024-05-11 17:06:04       33 阅读
  2. C#系列-C#EF框架实现增删(27)

    2024-05-11 17:06:04       52 阅读
  3. SpringBoot实现增删

    2024-05-11 17:06:04       40 阅读
  4. MyBatisPlus实现增删

    2024-05-11 17:06:04       22 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-11 17:06:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-11 17:06:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-11 17:06:04       87 阅读
  4. Python语言-面向对象

    2024-05-11 17:06:04       96 阅读

热门阅读

  1. .NET_控制反转简述

    2024-05-11 17:06:04       37 阅读
  2. No signature found in package of version 2 or newer for package

    2024-05-11 17:06:04       26 阅读
  3. go-Expect-实验

    2024-05-11 17:06:04       35 阅读
  4. Linux 第二十六章

    2024-05-11 17:06:04       33 阅读
  5. vue3-seamless-scroll实现循环滚动

    2024-05-11 17:06:04       27 阅读
  6. 以太网网络变压器型号

    2024-05-11 17:06:04       29 阅读
  7. git 更换远程仓库地址三种方法总结

    2024-05-11 17:06:04       28 阅读
  8. 【笔记】Android MVNO APN 字段配置方法

    2024-05-11 17:06:04       27 阅读
  9. react18【系列实用教程】useState (2024最新版)

    2024-05-11 17:06:04       38 阅读
  10. impdp恢复表后发现比原表多了100多行

    2024-05-11 17:06:04       32 阅读
  11. C++ 多线程笔记1 线程的创建

    2024-05-11 17:06:04       30 阅读
  12. 【Python快速上手(十九)】

    2024-05-11 17:06:04       30 阅读