本帖最后由 jason9031 于 2019-3-15 10:52 编辑
【小工具代码c#数据库表 字段 对比】生产Prod 和测试Test 数据库表+ 字段(最多15个不同)不同对比
先把所有表查出来,正向反向遍历看表是否在对方列表中存在,信息都记录在datatable中
只有在表名相同的前提下才正反向遍历字段是否互相存在,代码写的有点啰嗦,凑合用吧
private void btnCompare_Click(object sender, EventArgs e)
{
int iTableNumCount = 1;
DataTable dtAllResult = new DataTable();
dtAllResult.Columns.Add("Number");
dtAllResult.Columns.Add("TableName");
dtAllResult.Columns.Add("Result_Prod");
dtAllResult.Columns.Add("Result_Test");
dtAllResult.Columns.Add("Field_Prod1");
dtAllResult.Columns.Add("Field_Prod2");
dtAllResult.Columns.Add("Field_Prod3");
dtAllResult.Columns.Add("Field_Prod4");
dtAllResult.Columns.Add("Field_Prod5");
dtAllResult.Columns.Add("Field_Prod6");
dtAllResult.Columns.Add("Field_Prod7");
dtAllResult.Columns.Add("Field_Prod8");
dtAllResult.Columns.Add("Field_Prod9");
dtAllResult.Columns.Add("Field_Prod10");
dtAllResult.Columns.Add("Field_Prod11");
dtAllResult.Columns.Add("Field_Prod12");
dtAllResult.Columns.Add("Field_Prod13");
dtAllResult.Columns.Add("Field_Prod14");
dtAllResult.Columns.Add("Field_Prod15");
dtAllResult.Columns.Add("Field_Test1");
dtAllResult.Columns.Add("Field_Test2");
dtAllResult.Columns.Add("Field_Test3");
dtAllResult.Columns.Add("Field_Test4");
dtAllResult.Columns.Add("Field_Test5");
dtAllResult.Columns.Add("Field_Test6");
dtAllResult.Columns.Add("Field_Test7");
dtAllResult.Columns.Add("Field_Test8");
dtAllResult.Columns.Add("Field_Test9");
dtAllResult.Columns.Add("Field_Test10");
dtAllResult.Columns.Add("Field_Test11");
dtAllResult.Columns.Add("Field_Test12");
dtAllResult.Columns.Add("Field_Test13");
dtAllResult.Columns.Add("Field_Test14");
dtAllResult.Columns.Add("Field_Test15");
bool bIfHaveSameTable = false;
DataTable dtDifferent = new DataTable();
string sqlAllTable = " SELECT TABLE_NAME FROM ALL_TABLES ";
DataTable dtProdTable = Tools.OracleHelper.ExecuteDataTable(Tools.OracleHelper.DataBaseType.DB_IMES, sqlAllTable);
DataTable dtTestTable = Tools.OracleHelper.ExecuteDataTable(Tools.OracleHelper.DataBaseType.DB_IMES_TEST, sqlAllTable);
string sqlAllTableCount = " SELECT COUNT(*) FROM ALL_TABLES ";
string strPordCount = Tools.OracleHelper.ExecuteScalarToString(Tools.OracleHelper.DataBaseType.DB_IMES, sqlAllTableCount);
string strTestCount = Tools.OracleHelper.ExecuteScalarToString(Tools.OracleHelper.DataBaseType.DB_IMES_TEST, sqlAllTableCount);
string strSqlTableStructHead = " SELECT * FROM ";
string strSqlTableStructTail = " WHERE ROWNUM < 2";
for (int icount = 0; icount < dtProdTable.Rows.Count; icount++)
{
bIfHaveSameTable = false;
for (int icountTest = 0; icountTest < dtTestTable.Rows.Count; icountTest++)
{
if (dtProdTable.Rows[icount][0].ToString().Trim().Equals(dtTestTable.Rows[icountTest][0].ToString().Trim()))
{
//有相同表名
bIfHaveSameTable = true;
break;
}
}
string nowProdTable = dtProdTable.Rows[icount][0].ToString().Trim();
if (bIfHaveSameTable)//有相同表 再比较表结构
{
DataTable dtProdTableField = new DataTable();
DataTable dtTestTableField = new DataTable();
try
{
dtProdTableField = Tools.OracleHelper.ExecuteDataTable(Tools.OracleHelper.DataBaseType.DB_IMES, strSqlTableStructHead + nowProdTable + strSqlTableStructTail);
dtTestTableField = Tools.OracleHelper.ExecuteDataTable(Tools.OracleHelper.DataBaseType.DB_IMES_TEST, strSqlTableStructHead + nowProdTable + strSqlTableStructTail);
}
catch (Exception ex)
{
string str = ex.ToString();
DataRow drTempErr = dtAllResult.NewRow();
drTempErr["Number"] = (iTableNumCount++).ToString();
drTempErr["TableName"] = (nowProdTable).ToString();
drTempErr["Result_Prod"] = ("Error").ToString();
drTempErr["Result_Test"] = ("Error").ToString();
dtAllResult.Rows.Add(drTempErr);
continue;
}
bool bFiledSame = false;
int iProdMoreThanTest = 1;
DataRow drTemp = dtAllResult.NewRow();
drTemp["Number"] = (iTableNumCount++).ToString();
drTemp["TableName"] = (nowProdTable).ToString();
drTemp["Result_Prod"] = ("Have").ToString();
drTemp["Result_Test"] = ("Have").ToString();
//drTemp["Field_Prod1"] = (iTableNumCount++).ToString();
//drTemp["Field_Test1"] = (iTableNumCount++).ToString();
foreach (DataColumn dcProd in dtProdTableField.Columns)
{
bFiledSame = false;
foreach (DataColumn dcTest in dtTestTableField.Columns)
{
if (dcTest.ColumnName.ToString().Trim().Equals(dcProd.ColumnName.ToString().Trim()))
{
bFiledSame = true;
break;
}
}
if (bFiledSame)
{
}
else
{
bFiledSame = false;
drTemp["Field_Prod"+ iProdMoreThanTest++] = (dcProd.ColumnName).ToString();
}
}
iProdMoreThanTest = 1;
int iTestMoreThanProd = 1;
foreach (DataColumn dcTest in dtTestTableField.Columns)
{
bFiledSame = false;
foreach (DataColumn dcProd in dtProdTableField.Columns)
{
if (dcTest.ColumnName.ToString().Trim().Equals(dcProd.ColumnName.ToString().Trim()))
{
bFiledSame = true;
break;
}
}
if (bFiledSame)
{
}
else
{
bFiledSame = false;
drTemp["Field_Test" + iTestMoreThanProd++] = (dcTest.ColumnName).ToString();
}
}
iTestMoreThanProd = 1;
dtAllResult.Rows.Add(drTemp);
}
else
{
DataRow drTemp = dtAllResult.NewRow();
drTemp["Number"] = (iTableNumCount++).ToString();
drTemp["TableName"] = (nowProdTable).ToString();
drTemp["Result_Prod"] = ("Have").ToString();
drTemp["Result_Test"] = ("No").ToString();
dtAllResult.Rows.Add(drTemp);
}
bIfHaveSameTable = false;
}
bIfHaveSameTable = false;
for (int icount = 0; icount < dtTestTable.Rows.Count; icount++)
{
bIfHaveSameTable = false;
for (int icountCheckFromProdToTest = 0; icountCheckFromProdToTest < dtAllResult.Rows.Count; icountCheckFromProdToTest++)
{
if (dtTestTable.Rows[icount][0].ToString().Trim().Equals(dtAllResult.Rows[icountCheckFromProdToTest]["TableName"].ToString().Trim()))
{
//有相同表名
bIfHaveSameTable = true;
break;
}
}
string strTestTableName = dtTestTable.Rows[icount][0].ToString().Trim();
if (bIfHaveSameTable)//有相同表 再比较表结构
{
//DataTable dtProdTableField = Tools.OracleHelper.ExecuteDataTable(Tools.OracleHelper.DataBaseType.DB_IMES, strSqlTableStructHead + strTestTableName + strSqlTableStructTail);
//DataTable dtTestTableField = Tools.OracleHelper.ExecuteDataTable(Tools.OracleHelper.DataBaseType.DB_IMES_TEST, strSqlTableStructHead + strTestTableName + strSqlTableStructTail);
//DataRow drTemp = dtAllResult.NewRow();
//drTemp["Number"] = (iTableNumCount++).ToString();
//drTemp["TableName"] = (strTestTableName).ToString();
//drTemp["Result_Prod"] = ("Have").ToString();
//drTemp["Result_Test"] = ("Have").ToString();
//dtAllResult.Rows.Add(drTemp);
}
else
{
DataRow drTemp = dtAllResult.NewRow();
drTemp["Number"] = (iTableNumCount++).ToString();
drTemp["TableName"] = (strTestTableName).ToString();
drTemp["Result_Prod"] = ("No").ToString();
drTemp["Result_Test"] = ("Have").ToString();
dtAllResult.Rows.Add(drTemp);
}
bIfHaveSameTable = false;
}
gctlData.DataSource = dtAllResult;
Export.ExportGridToFile(gvData, "TableDiff");
}
|