本文主要是介绍sqlsugar查询数据库下的所有表,批量修改表名字,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
查询数据库中的所有表
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);}}}
}
批量修改表名字
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);}}}}
}
执行之后
参考
这篇关于sqlsugar查询数据库下的所有表,批量修改表名字的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!