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>();
}
}
c# 遍历 Mysql 所有表所有列,查找目标数据的更多相关文章
- MySQL将表a中查询的数据插入到表b中
MySQL将表a中查询的数据插入到表b中 假设表b存在 insert into b select * from a; 假设表b不存在 create table b as select * from a ...
- MYSQL单表可以存储多少条数据???
MYSQL单表可以存储多少条数据??? 单表存储四千万条数据,说MySQL不行的自己打脸吧. 多说一句话,对于爬虫来说,任何数据库,仅仅是存储数据的地方,最关心的是 能否存储数据和存储多少数据以及存储 ...
- mysql修改表和列
mysql修改列 mysql增加列,修改列名.列属性,删除列语句 mysql修改表名,列名,列类型,添加表列,删除表列 alter table test rename test1; --修 ...
- MySql给表添加列和注释
1.给表添加列 ALTER TABLE supplier_seller ADD COLUMN company_id INT NULL COMMENT '供应主体id'; 默认情况下,添加的列会添加到最 ...
- Mysql 数据库 表中列的操作
[1]Mysql数据库中表的列操作 Mysql中关于表中列的操作集语句: -- [1]增加一列 ) DEFAULT NULL COMMENT '目的码区号'; -- [2]增加一列,在dnis_are ...
- MySQL之表、列别名及各种JOIN连接详解
MySQL在SQL中,合理的别名可以让SQL更容易以及可读性更高.别名使用as来表示,可以分为表别名和列别名,别名应该是先定义后使用才对,所以首先要了解sql的执行顺序(1) from(2) on(3 ...
- mysql建表以及列属性
一.整型( int, tinyint, smallint 等 ) ------------------------------------------------------------------- ...
- mysql查看表中列信息
查看所有数据库中所有表的数据库名和表名 SELECT `TABLES`.`TABLE_SCHEMA`, `TABLES`.`TABLE_NAME` FROM `information_schema`. ...
- MYSQL 获取表的列信息
SELECT COLUMN_NAME as '列名' ,DATA_TYPE as '字段类型' ,COLUMN_TYPE as '长度加类型' FROM information_schema.`COL ...
随机推荐
- CSS世界中的“盒子”
1.块级元素 HTML标签通常被分为两类:块级元素和内联元素. “块级元素”和“display为block的元素”不是同一个概念.例如<li>元素默认的display值为list-item ...
- RabbitMQ ——与Spring集成及exchange的direct、topic方式实现和简单队列实现
程序整体结构 Maven依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http: ...
- 判断是否为PC
function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = ["Android", " ...
- 【leetcode】399. Evaluate Division
题目如下: Equations are given in the format A / B = k, whereA and B are variables represented as strings ...
- python在windows中运行文件
"d:Program Files\python35\python.exe" hello.txt
- linux0.11内核源码——boot和setup部分
https://blog.csdn.net/KLKFL/article/details/80730131 https://www.cnblogs.com/joey-hua/p/5528228.html ...
- 汉诺塔IX
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=76447#problem/E 汉诺塔IX Time Limit:1000MS Me ...
- 防止NSTimer和调用对象之间的循环引用
防止NSTimer和调用对象之间的循环引用 @interface NSTimer (EOCBlocksSupport) + (NSTimer *)eoc_scheduledTimerWithTimeI ...
- pytest_用例运行级别_class级
''' 模块级(setup_module/teardown_module)开始于模块始末, 全局的在类中不起作用 类级(setup_class/teardown_class)只在类中前后运行一次(在 ...
- 二次封装arcgis的timeslider
arcgis的timeslider是对dojo slider二次封装,项目需要,所有Map用统一样式的slider,所以写了一个common的dojo class,统一调用生成slider,作为对ti ...