简体   繁体   中英

C# DataTables - Design Pattern Implementation

Problem Statement

I am working on a web application in Asp.Net. It has multiple modules and each modules have multiple C# DataTables which are being populated through DataReader and DataAdapter .

So for this purpose I have to initialize Datatable, specify its columns names and their DataType for each and every DataTable. Although in some cases, they contain my DataBase's Tables Column Name but while Traversing the DataTables, I have to specify columns which I want to Select and to get Data from Selected Row I have to specify Column Name explicitly.

It is very time consuming and I have to write so much of code for these operation in each module.

My requirement is that I want a Design Pattern (Like we implement Repository Pattern in MVC, etc) for DataTables which implement C# DataTables and provides Property accessors for Initialization of DataTables, Number of Columns of each DataTable, Columns Names and for traversing as well. Each DataTable should be independent of others.

Possible Solution

I have found that Adapter pattern is suitable for it. But I couldn't figure it out how to use it for C# DataTables in these specific scenarios so far.

any help would be greatly appreciated..

Try following code

static DataTable MakeTable(Dictionary<string, Type> dict)
{
    DataTable dt = new DataTable();
    foreach(KeyValuePair<string, Type> row in dict)
    {
        dt.Columns.Add(row.Key, row.Value);
    }
    return dt;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM