国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > SQL Server > 正文

SQL Server數據庫中的表名稱、字段比較

2020-07-25 12:35:56
字體:
來源:轉載
供稿:網友

前言

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

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

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

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

  。。。。。

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

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

控制臺應用程序,目前只能對比新增,修改(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; } }}

測試結果

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對武林網的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西昌市| 垫江县| 集贤县| 宁武县| 新疆| 年辖:市辖区| 邳州市| 土默特右旗| 新乐市| 三原县| 巴马| 林芝县| 疏勒县| 鲁甸县| 云南省| 阳东县| 登封市| 旅游| 城步| 东乌珠穆沁旗| 雷波县| 阳高县| 监利县| 大城县| 兖州市| 临泽县| 兴仁县| 方正县| 绥化市| 黔东| 尼勒克县| 勃利县| 改则县| 沁阳市| 贵港市| 左贡县| 定兴县| 镶黄旗| 庆安县| 沂水县| 嘉祥县|