sqlsugar查询数据库下的所有表,批量修改表名字

查询数据库中的所有表

cs 复制代码
using SqlSugar;

namespace 批量修改数据库表名
{
    internal class Program
    {
        static void Main(string[] args)
        {
            SqlSugarClient sqlSugarClient = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=testchangetablename;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False",
                DbType = DbType.SqlServer,
                IsAutoCloseConnection = true
            });
            //创建四个表
            /*
            use testchangetablename;
            create table test01_001(
	            Id bigint primary key not null
            )
            create table test01_002(
	            Id bigint primary key not null
            )
            create table test01_003(
	            Id bigint primary key not null
            )
            create table test01_004(
	            Id bigint primary key not null
            )
             */
            var tables = sqlSugarClient.DbMaintenance.GetTableInfoList(false);//true 走缓存 false不走缓存
            foreach (DbTableInfo item in tables)
            {
                //表名字
                Console.WriteLine(item.Name);
                //描述
                Console.WriteLine(item.Description);
            }
        }
    }
}

批量修改表名字

cs 复制代码
using SqlSugar;

namespace 批量修改数据库表名
{
    internal class Program
    {
        static void Main(string[] args)
        {
            SqlSugarClient sqlSugarClient = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=testchangetablename;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False",
                DbType = DbType.SqlServer,
                IsAutoCloseConnection = true
            });
            var tables = sqlSugarClient.DbMaintenance.GetTableInfoList(false);//true 走缓存 false不走缓存
            foreach (DbTableInfo item in tables)
            {
                if (item.Name.StartsWith("test01_"))
                {
                    var newName = item.Name.Replace("test01_", "test02_");
                    sqlSugarClient.DbMaintenance.RenameTable(item.Name, newName);
                }
            }
        }
    }
}

执行之后

参考

相关推荐
硅基喵1 个月前
ABP DTO 与对象映射:AutoMapper Profile 配置与 Mapperly 源生成器
dotnet
硅基喵1 个月前
ABP 应用服务源码解读:ApplicationService 基类内置了哪些能力
dotnet
硅基喵1 个月前
ABP 仓储模式源码解读:自动过滤是如何实现的
dotnet
硅基喵1 个月前
ABP DDD 实体源码解读:7 层基类的设计逻辑和适用场景
dotnet
硅基喵1 个月前
ABP 模块系统源码学习:启动时模块是如何加载和排序的
dotnet
qqxhb3 个月前
47|成本与性能:缓存、批处理、模型路由与降级
缓存·批处理·智能模型路由·多级降级预案·成本预算
xcLeigh4 个月前
IoTDB JDBC 完整使用教程:连接、查询、批处理与字符集配置
开发语言·数据库·qt·iotdb·查询·批处理·连接
硅基喵4 个月前
C# 也能像 Python 一样写脚本 | .NET 10 构建基于文件的应用
dotnet
硅基喵4 个月前
.NET 10 使用 Microsoft.AspNetCore.OpenApi 实现 API 版本管理
dotnet
weisian1514 个月前
进阶篇-LangChain篇-18--缓存与优化——语义缓存,批处理和模型降级策略
langchain·批处理·语义缓存·模型降级