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

执行之后

参考

相关推荐
課代表2 天前
bat 批处理中的路径:%CD%与%~dp0
脚本·bat·环境变量·目录·批处理·路径·相对路径
課代表3 天前
bat 批处理从文本文件自动创建文件夹
自动化·脚本·bat·批处理·txt·文件编码·文件夹创建
課代表5 天前
Windows 批处理 bat 变量扩展名
windows·命令行·bat·批处理·扩展名·递归遍历·后缀名
赵庆明老师6 天前
文件上传和存储
dotnet
Logic1017 天前
《Windows批处理(BAT)脚本实战大全:41个场景告别重复操作》含文件处理/查找/重命名/清理等)
windows·编程·文件管理·bat·效率工具·批处理·自动化脚本
課代表7 天前
bat 批处理文件中 PowerShell 命令换行问题
符号·参数·powershell·批处理·换行·续行符·管道符
課代表14 天前
PowerShell 字符转 UniCode 编码
字符编码·类型转换·unicode·powershell·批处理·转义·[char]
RestCloud14 天前
实时 vs 批处理:ETL在混合架构下的实践
数据仓库·etl·cdc·数据处理·批处理·数据传输·数据同步
課代表17 天前
批处理脚本密码保护之简单加密
脚本·密码·批处理··加密算法·xor加密·密码复杂度
課代表1 个月前
bat 批处理文件重命名加时间戳
时间·重命名·bat·时间戳·命令·批处理·字符串截取