/// <summary> /// copy 表和数据 /// </summary> /// <param name="newDbName">新数据库</param> /// <param name="oldDbName">旧数据库</param> /// <param name="tableName">表名</param> /// <param name="connStr">连接字符串</param> public void Copy(string newDbName, string oldDbName, string tableName, string connStr) { string constr = connStr; string tableSql = $"create table if not exists {newDbName}.{tableName} like {oldDbName}.{tableName}"; string strSql = $"insert {newDbName}.{tableName} select * from {oldDbName}.{tableName}"; using (MySqlConnection mycon = new MySqlConnection(constr)) { mycon.Open(); //这里加了if not exists(SQL语法很重要) using (MySqlCommand mycmd = new MySqlCommand(tableSql, mycon)) { object count = mycmd.ExecuteScalar(); }; //表结构有了,开始插入数据 using (MySqlCommand mycmd2 = new MySqlCommand(strSql, mycon)) { mycmd2.ExecuteNonQuery();//这一句或者上面那句都可以执行command } } }
MySql查询数据库所有表
select table_name from information_schema.tables where table_schema='数据库名称';