SQL Server数据库中的表名称、字段比较

所属分类: 数据库 / MsSql 阅读数: 1383
收藏 0 赞 0 分享

前言

项目中一般分测试环境(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; }

 }
}

测试结果

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。

更多精彩内容其他人还在看

对有insert触发器表取IDENTITY值时发现的问题

赶快查了下msdn,原来@@IDENTITY还有这么多讲究
收藏 0 赞 0 分享

SQL SERVER 查询正在实行的SQL语句

SQL SERVER 查询正在实行的SQL语句的实现代码
收藏 0 赞 0 分享

sql 随机抽取几条数据的方法 推荐

前段时间在做项目的时刻。总是遇到这样一个问题。就是要怎么去让首页显示的内容不断的变化。想了很久。也没有什么结果。后面去想了一下。得出以下一个结果
收藏 0 赞 0 分享

sql 多条件组合查询,并根据指定类别找出所有最小子类别的SQL语句备忘

多条件组合查询,并根据指定类别找出所有最小子类别的SQL语句备忘
收藏 0 赞 0 分享

Java 实现连接sql server 2000

JDBC技术事实上是一种能通过JAVA语言访问任何结构化数据库的应用程序接口(API)(Sun这样说的,我也不知道是不是真的),而且现在的JDBC 3.0据Sun说也能访问Execel等电子表格程序!
收藏 0 赞 0 分享

SQL Server 不存在或访问被拒绝(转)

在使用 SQL Server 的过程中,用户遇到最多的问题莫过于连接失败了。一般而言,有两种连接SQL Server 的方式,一是利用 SQL Server 自带的客户端工具
收藏 0 赞 0 分享

分页查询 效率最高

给大家分享个效率最高的分页查询 5000万级别有效 比 ROWNUMBER 和Top效率高
收藏 0 赞 0 分享

sqlserver 系统存储过程 中文说明

sqlserver 系统存储过程这样大家就知道这些存储过程的作用了。
收藏 0 赞 0 分享

sql 多表连接查询

sql 多表连接查询语句代码,大家可以参考下。
收藏 0 赞 0 分享

SQL SERVER 自增列

判断Table是否存在自增列(Identity column)
收藏 0 赞 0 分享
查看更多