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);
                }
            }
        }
    }
}

执行之后

参考

相关推荐
方永锐11 天前
切换笔记本键盘的启用与禁用状态
windows·笔记本电脑·键盘·批处理
老肖相当外语大佬1 个月前
主观与客观,破除DDD凭经验魔咒
java·ddd·领域驱动设计·dotnet
老肖相当外语大佬1 个月前
DDD是软件工程的第一性原理?
java·ddd·领域驱动设计·dotnet·软件设计
老肖相当外语大佬1 个月前
DDD建模后写代码的正确姿势(Java、dotnet双平台)
java·c#·ddd·领域驱动设计·dotnet
baiyu333 个月前
成为CMake砖家(4): VSCode中的CMake语法高亮
vscode·cmake·dotnet
VAllen3 个月前
【一天一点.NET小知识】运用向量Vector<T>加速求和计算
c#·.net·.net core·dotnet·csharp
Flamesky3 个月前
Dotnet算法与数据结构:Hashset, List对比
list·dotnet·hashset
码上飞扬3 个月前
Windows批处理入门:快速掌握批处理脚本的基本技巧
windows·bat·批处理
VAllen3 个月前
为什么不推荐使用Linq?
linq·dotnet·csharp
VAllen4 个月前
如何使用csproj构建C#源代码组件NuGet包?
dotnet·nuget