SQL Server数据库中表名称、字段比较的示例分析

34次阅读
没有评论

共计 3661 个字符,预计需要花费 10 分钟才能阅读完成。

这篇文章主要为大家展示了“SQL Server 数据库中表名称、字段比较的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让丸趣 TV 小编带领大家一起研究并学习一下“SQL Server 数据库中表名称、字段比较的示例分析”这篇文章吧。

前言

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

测试结果

以上是“SQL Server 数据库中表名称、字段比较的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注丸趣 TV 行业资讯频道!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-01发表,共计3661字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)