• c# 遍历 Mysql 所有表所有列,查找目标数据


    在 Mysql 的 information_schema 库中 COLUMNS 表中存放了所有表的所有列。

    using MySql.Data.MySqlClient;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<MyTable> list = GetTableList();
                Query(list, "1111aaaa");
                Console.WriteLine("over");
                Console.ReadLine();
            }
    
            static List<MyTable> GetTableList()
            {
                using (MySqlConnection conn = GetConnection())
                {
                    Dictionary<string, MyTable> dic = new Dictionary<string, MyTable>();
                    MySqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "select table_name, column_name from information_schema.columns where table_schema = 'lpet6plusdb';";
                    using (MySqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string table = reader.GetString("table_name");
                            string column = reader.GetString("column_name");
                            if (dic.ContainsKey(table))
                            {
                                dic[table].ColumnList.Add(column);
                            }
                            else
                            {
                                MyTable t = new MyTable();
                                t.Table = table;
                                t.ColumnList.Add(column);
                                dic.Add(t.Table, t);
                            }
                        }
                    }
                    return dic.Values.ToList();
                }
            }
    
            static void Query(List<MyTable> list, string str)
            {
                using (MySqlConnection conn = GetConnection())
                {
                    MySqlCommand cmd = conn.CreateCommand();
                    foreach (MyTable table in list)
                    {
                        foreach (string column in table.ColumnList)
                        {
                            cmd.CommandText = string.Format("select count(*) from {0} where `{1}` like '%{2}%'", table.Table, column, str);
                            object obj = cmd.ExecuteScalar();
                            if (Convert.ToInt32(obj) > 0)
                            {
                                Console.WriteLine(string.Format("TableName: {0}, ColumnName: {1}", table.Table, column));
                            }
                        }
                    }
                }
            }
    
            static MySqlConnection GetConnection()
            {
                MySqlConnection conn = new MySqlConnection("server=localhost;port=3306;user id=userid;password=pass;database=mydb;pooling=true;ConnectionTimeout=1800");
                conn.Open();
                return conn;
            }
        }
    
        public class MyTable
        {
            public string Table { get; set; }
            public List<string> ColumnList { get; set; } = new List<string>();
        }
    }
    

      

  • 相关阅读:
    memmove 的实现
    [转]SGI STL 红黑树(Red-Black Tree)源代码分析
    [转]让我看了很有感触
    [转]C++ list 类学习笔记
    [转]码农自白:这样成为谷歌工程师
    [转]Traits 编程技法+模板偏特化+template参数推导+内嵌型别编程技巧
    泛型指针,原生指针和智能指针
    [转]C++基本功和 Design Pattern系列 ctor & dtor
    python+opencv滤波操作
    python+opencv阈值
  • 原文地址:https://www.cnblogs.com/aitong/p/12022060.html
Copyright © 2020-2023  润新知