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

执行之后

参考

相关推荐
xcLeigh8 天前
IoTDB JDBC 完整使用教程:连接、查询、批处理与字符集配置
开发语言·数据库·qt·iotdb·查询·批处理·连接
硅基喵19 天前
C# 也能像 Python 一样写脚本 | .NET 10 构建基于文件的应用
dotnet
硅基喵20 天前
.NET 10 使用 Microsoft.AspNetCore.OpenApi 实现 API 版本管理
dotnet
weisian15120 天前
进阶篇-LangChain篇-18--缓存与优化——语义缓存,批处理和模型降级策略
langchain·批处理·语义缓存·模型降级
硅基喵1 个月前
ASP.NET Core 内存缓存实战:一篇搞懂该怎么配、怎么避坑
dotnet
achi0102 个月前
Apache Beam 详细入门指南
etl·批处理·流处理·apache beam·dataflow 模型·pcollection·批流融合
ChaITSimpleLove2 个月前
aiagent-webapi 命令的详细使用说明
dotnet·webapi·ai agent·agent framework·maf·projecttemp
TeamDev2 个月前
使用 Docker 部署 DotNetBrowser 应用程序
运维·ui·docker·容器·桌面应用·dotnet·dotnetbrowser
CSharp精选营2 个月前
.NET命名之谜:它与C#纠缠20年的关系揭秘
c#·.net·dotnet·csharp
VAllen2 个月前
ConcurrentNativeQueue<T>:一个使用 .NET 实现的零 GC 压力的无锁 MPSC 原生队列
c#·.net·性能测试·.net core·dotnet·csharp