oracle别比对句,Oracle根据实体类比对2个数据库结构差异(demo)

usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingCommonHelper;usingDapper;usingDapperExtensions;usingNewtonsoft.Json;namespaceDBHelper

{classProgram

{//实体模型路径

public static string modelpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DAO");//实际数据结构获取源

public const string sourcedbconnStr = "Data Source=192.168.200.151/orcl;User ID=BG;Password=1;";//要比较的数据库连接串

public const string todbconnStr = "Data Source=192.168.200.151/orcl;User ID=BG2019;Password=1;";//缓存数据结构对象 如果存在 则不再去sourcedb中获取结构

public static string SaveFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sourceList.txt");//生成的添加字段语句

public static string SqlSaveFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sql.txt");public static ListGetAllTableName()

{

DirectoryInfo di= newDirectoryInfo(modelpath);var res =new List();foreach (var item in di.GetFiles("*.cs"))

{if (!item.Name.ToLower().StartsWith("base"))

res.Add(item.Name.Replace(item.Extension,""));

}returnres;

}static void Main(string[] args)

{//记录所有的源数据库 数据结构

List sl = new List();var tableNames =GetAllTableName();if(File.Exists(SaveFilePath))

{using (FileStream fs = newFileStream(SaveFilePath, FileMode.Open))

{using (StreamReader sw = newStreamReader(fs))

{var ss =sw.ReadToEnd();

sl= JsonConvert.DeserializeObject>(ss);

}

}

}else{var sourceDB = newDapperHelper(sourcedbconnStr);foreach (var item intableNames)

{var source =sourceDB.Conn.Query(

$"select column_name,data_type,data_length from user_tab_cols where table_name='{item}'")

.ToList();

sl.Add(new SourceSaveList() { cols = source, tableName =item });

}

}var toDB = newDapperHelper(todbconnStr);

StringBuilder sb= newStringBuilder();foreach (var item intableNames)

{var source = sl.First(c => c.tableName ==item).cols;var to=toDB.Conn.Query($"select column_name,data_type,data_length from user_tab_cols where table_name='{item}'")

.ToList();if (source.Count == to.Count) continue;foreach (var source_column insource)

{if (to.Any(c => c.COLUMN_NAME == source_column.COLUMN_NAME)) continue;switch(source_column.DATA_TYPE)

{case "DATE":

sb.Append($"alter table {item} add {source_column.COLUMN_NAME} DATE;");break;case "TIMESTAMP(6)":

sb.Append($"alter table {item} add {source_column.COLUMN_NAME} TIMESTAMP(6);");break;case "TIMESTAMP":

sb.Append($"alter table {item} add {source_column.COLUMN_NAME} TIMESTAMP({source_column.DATA_LENGTH});");break;default:

sb.Append($"alter table {item} add {source_column.COLUMN_NAME} {source_column.DATA_TYPE}({source_column.DATA_LENGTH});");break;

}

sb.Append("\r\n");

}

}using (FileStream fs = newFileStream(SaveFilePath, FileMode.Create))

{using (StreamWriter sw = newStreamWriter(fs))

{

sw.WriteLine(JsonConvert.SerializeObject(sl));

}

}using (FileStream fs = newFileStream(SqlSaveFilePath, FileMode.Create))

{using (StreamWriter sw = newStreamWriter(fs))

{

sw.WriteLine(sb.ToString());

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值