C# GetSchema Get List of Table 获取数据库中所有的表名以及表中的纪录条数的方法
2011-10-18 20:27 通用C#系统架构 阅读(3826) 评论(4) 编辑 收藏 举报
数据库连接如下:
<add key="BusinessDbConnection" value="Data Source=localhost;Initial Catalog=PRM;User Id = sa ; Password = "@sa;" />
代码如下:
//--------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2011 , Hairihan TECH, Ltd.
//--------------------------------------------------------------------
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Example
{
using DotNet.BaseManager;
using DotNet.Utilities;
using DotNet.DbUtilities;
public class SchemaTable
{
public void GetSchemaTable()
{
SqlConnection connection = new SqlConnection(BaseSystemInfo.BusinessDbConnection);
connection.Open();
DataTable dt = connection.GetSchema("Tables");
connection.Close();
foreach (DataRow dataRow in dt.Rows)
{
string tableName = dataRow["TABLE_NAME"].ToString();
string commandText = "SELECT COUNT(*) FROM " + tableName;
int rowCount = int.Parse(DbHelper.ExecuteScalar(commandText).ToString());
if (rowCount > 1000)
{
System.Console.WriteLine("{0,8} {1}", rowCount, tableName);
}
}
}
}
}
// All Rights Reserved , Copyright (C) 2011 , Hairihan TECH, Ltd.
//--------------------------------------------------------------------
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Example
{
using DotNet.BaseManager;
using DotNet.Utilities;
using DotNet.DbUtilities;
public class SchemaTable
{
public void GetSchemaTable()
{
SqlConnection connection = new SqlConnection(BaseSystemInfo.BusinessDbConnection);
connection.Open();
DataTable dt = connection.GetSchema("Tables");
connection.Close();
foreach (DataRow dataRow in dt.Rows)
{
string tableName = dataRow["TABLE_NAME"].ToString();
string commandText = "SELECT COUNT(*) FROM " + tableName;
int rowCount = int.Parse(DbHelper.ExecuteScalar(commandText).ToString());
if (rowCount > 1000)
{
System.Console.WriteLine("{0,8} {1}", rowCount, tableName);
}
}
}
}
}
希望能起点而参考作用.
将权限管理、工作流管理做到我能力的极致,一个人只能做好那么很少的几件事情。