连接字符串
Server=address;Database=dataBaseName;Uid=username;Pwd=password;
检查表是否存在
方法1
SELECT *
FROM information_schema.tables
WHERE table_schema = 'yourdbName'
AND table_name = 'testtable'
LIMIT 1;
(默认table_schema 即数据库名称)
方法2
SELECT count(*)
FROM information_schema.tables
WHERE (TABLE_SCHEMA = 'your_db_name') AND (TABLE_NAME = 'name_of_table')
参考资料:
Check if table exists without using “select from”
获得表的自增长id当前最大值
select max(id) from tableName
参考资料:
Mysql获取最大自增ID(auto_increment)的五种方式及其特点
获取表的字段列表(EF)
string conn= "Server=address;Database=dataBaseName;Uid=username;Pwd=password;";
using(var _con= new MySqlConnection(conn))
{
var lst = new List<string>();
var sql = "select * from " + tableName + " where 1<0";
using (var cmd = new MySqlCommand(sql, _con))
{
using (var r = cmd.ExecuteReader())
{
for (int i = 0; i < r.FieldCount; ++i)
{
lst.Add(r.GetName(i));
}
}
}
}