简体   繁体   English

C#DataTables-设计模式实现

[英]C# DataTables - Design Pattern Implementation

Problem Statement 问题陈述

I am working on a web application in Asp.Net. 我正在Asp.Net中的Web应用程序上工作。 It has multiple modules and each modules have multiple C# DataTables which are being populated through DataReader and DataAdapter . 它具有多个模块,每个模块具有多个C# DataTables ,这些C# DataTables通过DataReaderDataAdapter进行填充。

So for this purpose I have to initialize Datatable, specify its columns names and their DataType for each and every DataTable. 因此,为此,我必须初始化Datatable,为每个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. 尽管在某些情况下,它们包含我的DataBase's Tables Column Name但是在遍历数据DataBase's Tables Column Name时,我必须指定要选择的列并从选定行中获取数据,我必须明确指定列名。

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. 我的要求是我想要一个数据表的设计模式(就像我们在MVC中实现存储库模式等),该模式实现C#数据表并为数据表的初始化,每个数据表的列数,列名以及遍历提供Property accessors 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. 但是到目前为止,我仍无法弄清楚如何在C#DataTables中使用它。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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