C#获取MySQL数据库中的所有的表名和字段名及字段的数据类型
2014-11-17 17:27
2907 查看
获取表名
public bool GetTableName()
{
string sql = "show tables;";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader reader = null;
try
{
if (!openconn()) return false;
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
list_tblName = new List<string>();
while (reader.Read())
{
string t = reader.GetString(0);
list_tblName.Add(t);
}
}
reader.Close();
closeconn();
return true;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
reader.Close();
closeconn();
return false;
}
}
获取一张表中的所有字段(列)名和字段数据类型
public bool GetColNameType(string _params)
{
MySqlCommand cmd = null;
MySqlDataReader reader = null;
List<string> list_ColName = new List<string>();
List<Type> list_ColType = new List<Type>();
string sql = "show columns from " + _params + " ;";
if (!openconn()) { return false; }
cmd = new MySqlCommand(sql, conn);
try
{
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
string t = reader.GetString(0);
Type tt = reader.GetValue(1) as Type;
string ttt = reader.GetString(1);
list_ColName.Add(t);
list_ColType.Add(tt);
coltype.Add(ttt);
}
}
reader.Close();
closeconn();
return true;
}
catch (Exception e) { return false; }
}
以下是网上搜到的方法:
查询数据库中所有表名
select table_name from information_schema.tables where table_schema='csdb' and table_type='base table';
查询指定数据库中指定表的所有字段名column_name
select column_name from information_schema.columns where table_schema='csdb' and table_name='users'
public bool GetTableName()
{
string sql = "show tables;";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader reader = null;
try
{
if (!openconn()) return false;
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
list_tblName = new List<string>();
while (reader.Read())
{
string t = reader.GetString(0);
list_tblName.Add(t);
}
}
reader.Close();
closeconn();
return true;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
reader.Close();
closeconn();
return false;
}
}
获取一张表中的所有字段(列)名和字段数据类型
public bool GetColNameType(string _params)
{
MySqlCommand cmd = null;
MySqlDataReader reader = null;
List<string> list_ColName = new List<string>();
List<Type> list_ColType = new List<Type>();
string sql = "show columns from " + _params + " ;";
if (!openconn()) { return false; }
cmd = new MySqlCommand(sql, conn);
try
{
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
string t = reader.GetString(0);
Type tt = reader.GetValue(1) as Type;
string ttt = reader.GetString(1);
list_ColName.Add(t);
list_ColType.Add(tt);
coltype.Add(ttt);
}
}
reader.Close();
closeconn();
return true;
}
catch (Exception e) { return false; }
}
以下是网上搜到的方法:
查询数据库中所有表名
select table_name from information_schema.tables where table_schema='csdb' and table_type='base table';
查询指定数据库中指定表的所有字段名column_name
select column_name from information_schema.columns where table_schema='csdb' and table_name='users'
相关文章推荐
- C# 如何获取SQL Server 中指定数据表的所有字段名和字段类型
- SQL获取指定数据表所有字段类型和精度脚本
- SQL获取指定数据表所有字段类型和精度脚本
- 怎么获取一个类型的所有字段的名字 和获取给予数据相应的值
- SQL获取指定数据表所有字段类型和精度脚本
- 获取一个网页数据返回的编码类型是gzip,解压后,网页中包含的中文字段变成了乱码,只需要把编码更改为BIG5 ,繁体字就正常显示了!
- 获取数据库所有表及其字段名称、类型、长度
- 用SQL语句获取MSSQL表关键字段的名称、数据类型和长度
- 获取Access数据里所有表的名称和表的字段
- C# 获取Excel列数据类型不一致的数据
- Oracle 数据字典(可用它动态获取字段名、长度、类型等)
- 使用JDBC4.0操作XML类型的字段(保存获取xml数据)的方法
- java 操作MySQL数据库的一个例子,包括表字段,类型,编码格式,表的所有值读取
- SQL获取所有用户名,数据库名、所有表名、所有字段名及字段类型
- 获取Access数据里所有表的名称和表的字段
- sql 获取指定数据表的所有字段名称的字符串
- c# 无法获取sybase中Text类型数据
- vb.net 获取数据表 字段名和数据类型
- ADO对象——获取数据库字段的数据类型
- 更新数据库所有表的某一个指定字段 ,附加对‘将 varchar 值转换为数据类型为 int 的列时发生语法错误’处理方法