问题
I'm trying to create a function to return all of the view names from a generic DbConnection. I'd like it to work for as many Database types as possible, but if I exclude some esoteric DB types, I don't mind.
In my current code, I have some special logic for Oracle, but I can't shake the feeling that my code doesn't handle things in a very robust manner:
Collection<string> Views
{
get
{
using (var dtViews = dbConnection.GetSchema("Views"))
// Determine the proper method to retrieve all tables and views
return ((dbType == dbTypeEnum.Oracle) ? GetAllViewsOracle(dtViews) : GetAllViews(dtViews));
}
}
static Collection<string> GetAllViews(DataTable dt)
{
return GetSingleDataTableCol(dt.Select("", "TABLE_NAME ASC"), dt.Columns["TABLE_NAME"]);
}
static Collection<string> GetAllViewsOracle(DataTable dtViews)
{
if (!dtViews.Columns.Contains("OWNER"))
return GetAllViews(dtViews);
if (!dtViews.Columns.Contains("VIEW_NAME"))
return new Collection<string>();
return GetSingleDataTableCol(dtViews.Select("OWNER NOT IN ('SYS','SYSTEM')", "VIEW_NAME ASC"), dtViews.Columns["VIEW_NAME"]);
}
protected static Collection<string> GetSingleDataTableCol(DataRow[] rows, DataColumn col)
{
var lCol = new List<string>();
if (col != null)
{
foreach (DataRow rowTable in rows)
{
lCol.Add(rowTable[col].ToString());
}
}
return lCol;
}
With Oracle, I filter the "SYS" and "SYSTEM" Schemas/Owners since Oracle has a bunch of special tables that I'll never use.
Ideally, I'd like to not even have to handle Oracle/others with special logic. Can I get all Views more generically than I currently handle it?
来源:https://stackoverflow.com/questions/5861443/retrieve-all-views-from-dbconnection-for-common-db-types