查询数据库中的所有表
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);
}
}
}
}
}
执行之后
参考