欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

SQLServer數(shù)據(jù)庫(kù)中表名稱、字段比較的示例分析

這篇文章主要為大家展示了“SQL Server數(shù)據(jù)庫(kù)中表名稱、字段比較的示例分析”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“SQL Server數(shù)據(jù)庫(kù)中表名稱、字段比較的示例分析”這篇文章吧。

本溪網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站設(shè)計(jì)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)于2013年創(chuàng)立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。

前言

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

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

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

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

  。。。。。

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

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

控制臺(tái)應(yīng)用程序,目前只能對(duì)比新增,修改(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; }

 }
}

測(cè)試結(jié)果

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

以上是“SQL Server數(shù)據(jù)庫(kù)中表名稱、字段比較的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

當(dāng)前題目:SQLServer數(shù)據(jù)庫(kù)中表名稱、字段比較的示例分析
文章分享:http://www.chinadenli.net/article34/pidcse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)網(wǎng)頁設(shè)計(jì)公司品牌網(wǎng)站制作網(wǎng)站導(dǎo)航軟件開發(fā)域名注冊(cè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)