Retrieve all Views from DbConnection (for common DB types)

て烟熏妆下的殇ゞ 提交于 2019-12-12 01:49:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!