C# 更新SQL Server数据库备注信息从另一数据库

Posted 空明流光

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 更新SQL Server数据库备注信息从另一数据库相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Diagnostics;

namespace SyncDatabaseColumnDescription
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var ssb = new SqlConnectionStringBuilder();
            ssb.DataSource = @".\SQL2008";
            ssb.UserID = "sa";
            ssb.Password = "firstelite";
            ssb.InitialCatalog = "OMSDB";
            using (var connection = new SqlConnection(ssb.ConnectionString))
            {
                connection.Open();
                var tableList = GetTableList(connection);


                    foreach (var table in tableList)
                    {

                        var fieldList = GetFieldList(connection, table);
                        foreach (var field in fieldList)
                        {
                            var desc = GetFieldDescription(connection, table, field);
                            if (desc != null)
                                Debug.Print(table + " -> " + field + ":" + desc);
                        }
                    }
            }
        }

        private void UpdateFieldDescription()
        {
            var ssb = new SqlConnectionStringBuilder();
            ssb.DataSource = @".\SQL2008";
            ssb.UserID = "**";
            ssb.Password = "****";
            ssb.InitialCatalog = "OMSDB_WITH_MEMO";
            using (var connection = new SqlConnection(ssb.ConnectionString))
            {
                connection.Open();
                var tableList = GetTableList(connection);

                ssb.InitialCatalog = "OMSDB";
                using (var connection2 = new SqlConnection(ssb.ConnectionString))
                {
                    connection2.Open();

                    var tableList2 = GetTableList(connection2);

                    foreach (var table in tableList)
                    {
                        if (!tableList2.Any(t => t == table))
                            continue;

                        var fieldList = GetFieldList(connection, table);
                        var fieldList2 = GetFieldList(connection2, table);

                        foreach (var field in fieldList)
                        {
                            if (!fieldList2.Any(f => f == field))
                                continue;

                            var desc = GetFieldDescription(connection, table, field);
                            if (string.IsNullOrWhiteSpace(desc))
                                continue;

                            var desc2 = GetFieldDescription(connection2, table, field);
                            if (desc2 != null)
                                UpdateFieldDescription(connection2, table, field, desc);
                            else
                                AddFieldDescription(connection2, table, field, desc);
                        }
                    }

                }
            }
        }

        private List<string> GetTableList(SqlConnection connection)
        {
            var tableList = new List<string>();
            using (var cmd = new SqlCommand(@"
                        SELECT TABLE_NAME 
                        FROM INFORMATION_SCHEMA.TABLES 
                        WHERE TABLE_TYPE = ‘BASE TABLE‘ 
                        ORDER BY TABLE_NAME", connection))
            {
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        tableList.Add(reader[0].ToString());
                    }
                }
            }
            return tableList;
        }

        private List<string> GetFieldList(SqlConnection connection, string tableName)
        {
            var fieldList = new List<string>();
            using (var cmd = new SqlCommand("SELECT * FROM [" + tableName + "] WHERE 1=2", connection))
            {
                using (var reader = cmd.ExecuteReader())
                {
                    for (var i = 0; i < reader.FieldCount; i++)
                        fieldList.Add(reader.GetName(i));
                }
            }
            return fieldList;
        }

        private string GetFieldDescription(SqlConnection connection, string tableName, string fieldName)
        {
            using (var cmd = new SqlCommand(@"SELECT g.[value]
                                            FROM    dbo.syscolumns A
                                                    INNER JOIN dbo.sysobjects D 
                                                        ON A.id = D.id
                                                            AND D.xtype = ‘U‘
                                                            AND D.name <> ‘dtproperties‘
                                                    INNER JOIN sys.extended_properties G 
                                                        ON A.id = G.major_id
                                                           AND A.colid = G.minor_id
                                            WHERE   D.name = ‘" + tableName + @"‘
                                                AND A.name=‘" + fieldName + @"", connection))
            {
                var ret = cmd.ExecuteScalar();
                if (ret == null)
                    return null;
                return ret.ToString();
            }
        }

        private bool AddFieldDescription(SqlConnection connection, string tableName, string filedName, string description)
        {
            using (var cmd = new SqlCommand(@"EXECUTE   sp_addextendedproperty   N‘MS_Description‘,   ‘" + description.Replace("","‘‘")
                + "‘,   N‘user‘,   N‘dbo‘,   N‘table‘,   N‘" + tableName + "‘,   N‘column‘,   N‘" + filedName + "", connection))
            {
                return cmd.ExecuteNonQuery() > 0;
            }
        }

        private bool UpdateFieldDescription(SqlConnection connection, string tableName, string filedName, string description)
        {
            using (var cmd = new SqlCommand(@"EXECUTE   sp_updateextendedproperty   N‘MS_Description‘,   ‘" + description.Replace("", "‘‘")
                + "‘,   N‘user‘,   N‘dbo‘,   N‘table‘,   N‘" + tableName + "‘,   N‘column‘,   N‘" + filedName + "", connection))
            {
                return cmd.ExecuteNonQuery() > 0;
            }
        }
    }
}

 

以上是关于C# 更新SQL Server数据库备注信息从另一数据库的主要内容,如果未能解决你的问题,请参考以下文章

SwiftUI:如何更新由一组静态数据驱动的列表并从另一组动态数据中提取信息位?

sql server,如何给表和字段添加备注,并更新备注,以及查询备注

如何从另一个表更新 Sql Server CE 表

SQL SERVER与C#的数据类型对应表

C#编写dll进行sql server数据库扩展储存过程

SQL Server更新表(用一张表的数据更新另一张表的数据)