人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

SQL Server數(shù)據(jù)庫中的表名稱、字段比較

發(fā)布日期:2021-12-10 17:03 | 文章來源:CSDN

項目中一般分測試環(huán)境(QAS),生產(chǎn)環(huán)境(PRD),當(dāng)我們的項目經(jīng)歷了一次周期跨度較長的更新后,當(dāng)我們發(fā)布到生產(chǎn)環(huán)境時,首要的任務(wù)是將新增的表,字段更新到生產(chǎn)數(shù)據(jù)庫。很多時候,當(dāng)我們發(fā)布更新的時候,已經(jīng)很難記得做了哪些變更。

當(dāng)然有的人會說,1.EF Code First 有history記錄,這是一種辦法,可靠么?不可靠。相信即便是用Code First,直接改數(shù)據(jù)庫的肯定不止我一個。

2.查看實體類變更記錄,這也是一個辦法。那如果用的DB First的呢?當(dāng)然也可以看,就是很麻煩。

3.開發(fā)過程中,對數(shù)據(jù)庫的變更記下來。這么做過的肯定也不止我一個。手動狗頭

。。。。。

中午的時候,就想著另外一個項目下個月要更新,改了N多的東西,到時候數(shù)據(jù)庫咋更新呢。就想著寫個工具比較兩個版本數(shù)據(jù)庫,表名稱,字段,字段類型的區(qū)別。

說干就干(本來想著用EF,DBContext應(yīng)該可以實現(xiàn),無奈學(xué)藝不精,最終還是回到了ADO.Net)。

控制臺應(yīng)用程序,目前只能對比新增,修改(SQl Server)。

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore;
namespace EFGetTable
{
 class Program
 {
  static void Main(string[] args)
  {
   string prdconnectionstring = "Data Source=localhost;initial catalog=ttPRD;user id=sa;password=password;MultipleActiveResultSets=True";
   var prd = GetTableNames(prdconnectionstring);
   string qasconnectionstring = "Data Source=localhost;initial catalog=ttqas;user id=sa;password=password;MultipleActiveResultSets=True";
   var qas = GetTableNames(qasconnectionstring);
   CompareTable(prd, qas);
  }
  public static List<TableInfo> GetTableNames(string connectionstr)
  {
   var tableresult = new List<TableInfo>();
   string sqlTableName = "Select * From Information_Schema.Tables";
   using (SqlConnection connection = new SqlConnection(connectionstr))
   {
    using (SqlCommand cmd = new SqlCommand(sqlTableName, connection))
    {
     try
     {
      connection.Open();
      SqlDataReader dr = cmd.ExecuteReader();//
      while (dr.Read())
      {
       // 表名
       TableInfo table = new TableInfo();
       table.TableName = dr["Table_Name"].ToString();

       table.columns.AddRange(GetColumnNamesByTable(dr["Table_Name"].ToString(), connection));
       tableresult.Add(table);
      }

      connection.Close();
     }
     catch (System.Data.SqlClient.SqlException e)
     {
      Console.ForegroundColor = ConsoleColor.Red;
      Console.Error.WriteLine(e.Message);
      connection.Close();
     }
    }
    return tableresult;
   }

  }
  public static List<CloumnInfo> GetColumnNamesByTable(string tableName, SqlConnection connection)
  {
   var Columnresults = new List<CloumnInfo>();
   string sqlcolum = $"Select * From Information_Schema.Columns t Where t.Table_Name =\'{tableName}\'";
   using (SqlCommand cmd = new SqlCommand(sqlcolum, connection))
   {
    SqlDataReader dr = cmd.ExecuteReader();//
    while (dr.Read())
    {
     // 表名
     CloumnInfo column = new CloumnInfo();
     column.CloumnName = dr["Column_name"].ToString();
     column.DateType = dr["DATA_TYPE"].ToString() + dr["CHARACTER_MAXIMUM_LENGTH"].ToString();
     Columnresults.Add(column);
    }
    return Columnresults;
   }
  }
  public static void CompareTable(List<TableInfo> prd, List<TableInfo> qas)
  {
   foreach (var p in qas)
   {
    var tablequery = prd.AsQueryable().Where(t => t.TableName.Equals(p.TableName));
    if (!tablequery.Any())
    {
     Console.WriteLine($"New Created Table {p.TableName}");
     continue;
    }
    else
    {
     var querytable = tablequery.FirstOrDefault();
     p.columns.ForEach(c =>
     {
      var Cloumnquery = querytable.columns.Select(cc => cc.CloumnName).Contains(c.CloumnName);
      if (!Cloumnquery)
      {
       Console.WriteLine($"New add cloumn: {c.CloumnName} on Table {p.TableName}");
      }
      else
      {
       var querycloumn = querytable.columns.Where(qt => qt.CloumnName.Equals(c.CloumnName)).FirstOrDefault();
       if (!querycloumn.DateType.Equals(c.DateType))
       {
        Console.WriteLine($"DateType Different: cloumn: {c.CloumnName} , {querycloumn.DateType}==>{c.DateType} on Table {p.TableName}");
       }
      }
     });
    }
   }
  }
 }
 public class TableInfo
 {
  public TableInfo()
  {
   columns = new List<CloumnInfo>();
  }
  public string TableName { get; set; }
  public List<CloumnInfo> columns { get; set; }
 }
 public class CloumnInfo
 {
  public string CloumnName { get; set; }
  public string DateType { get; set; }
 }
}

測試結(jié)果

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對本站的支持。

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部