如何将MySQL数据返回到泛型类

时间:2013-08-28 16:42:11

标签: c# mysql

我有很多课程,例如

    public class CountryModel
    {
        public string CountryCode { get; set; }
        public string CountryName { get; set; }
    }

    public class RegionModel
    {
        public string RegionId{ get; set; }
        public Int64 RegionName { get; set; }
        public string RegionType { get; set; }
    }

目前,我使用不同的存储过程来调用具有类属性相关数据的类。例如;

    while (CountryResultSet.Read())
    {
      CountryModel MyObj = new CountryModel();
      MyObj.CountryCode = oRes["CountryCode"].ToString();
      MyObj.CountryName = oRes["CountryName"].ToString();
    }

然后

    while (RegionResultSet.Read())
    {
      RegionModel MyRegionObj = new RegionModel();
      MyRegionObj.RegionId = oRes["RegionId "];
      MyRegionObj.RegionName = oRes["RegionName"].ToString();
      MyRegionObj.RegionType = oRes["RegionType"].ToString();
    }

这样可以正常工作,但是我需要多次重复这一过程,以便从MySQL数据中获取数据的不同请求。这似乎浪费了重复的代码。

任何人都可以推荐一种方法,这样我就可以使用一种可以满足所有类类型的生成方法。

1 个答案:

答案 0 :(得分:1)

我使用映射到数据库表和C#的Activator的代码类的组合。

以下内容用于内部开发工具,并使用从数据库中检索到的记录填充列表:

    public void FillDbObjectList<T>(out List<T> list, string tableName) where T : CommonUtilities.DbObjectTableBase
    {
        list = new List<T>();

        ADODB.Recordset rcd;
        this.FillRecordset(EDbRecordSource.Table, tableName, out rcd);

        while (!rcd.EOF)
        {
            object[] paramArray = this.FillActivatorParameterArray(rcd);
            list.Add((T)Activator.CreateInstance(typeof(T), paramArray));
            rcd.MoveNext();
        }

        this.TryCloseRecordset(rcd);
    }

FillActivatorParameterArray方法只是创建一个与字段顺序相关联的对象数组。

    private object[] FillActivatorParameterArray(ADODB.Recordset rcd)
    {
        object[] paramArray = new object[rcd.Fields.Count];

        for (int i = 0; i < rcd.Fields.Count; i++)
        {
            if (rcd.Fields[i].Value == DBNull.Value)
            {
                paramArray[i] = null;
            }
            else
            {
                paramArray[i] = rcd.Fields[i].Value;
            }
        }

        return paramArray;
    }

这是代码生成类之一的示例。

public class Table_Archetype : DbObjectTableBase
{
    #region Fields
    private System.String name = "ChangeMe";
    private System.Int32 fK_ToolField_ID_DataSource = 0;
    private System.String description__DEV = "";
    private System.Int32 fK_EnumArray_ID_DataExportType = 19;
    private System.Boolean isDeleted = false;
    #endregion

    public Table_Archetype() { }

    public Table_Archetype(System.Int32 inID) { base.id = inID; }

    public Table_Archetype(
        System.Int32 inID
        , System.String inName
        , System.Int32 inFK_ToolField_ID_DataSource
        , System.String inDescription__DEV
        , System.Int32 inFK_EnumArray_ID_DataExportType
        , System.Boolean inIsDeleted
    )
    {
        base.id = inID;
        this.name = inName;
        this.fK_ToolField_ID_DataSource = inFK_ToolField_ID_DataSource;
        this.description__DEV = inDescription__DEV;
        this.fK_EnumArray_ID_DataExportType = inFK_EnumArray_ID_DataExportType;
        this.isDeleted = inIsDeleted;
    }

    #region Properties
    public System.String Name
    {
        get { return this.name; }
        set
        {
            if (this.name != value)
            {
                this.name = value;
                this.isDirty = true;
            }
        }
    }
    public System.Int32 FK_ToolField_ID_DataSource
    {
        get { return this.fK_ToolField_ID_DataSource; }
        set
        {
            if (this.fK_ToolField_ID_DataSource != value)
            {
                this.fK_ToolField_ID_DataSource = value;
                this.isDirty = true;
            }
        }
    }
    public System.String Description__DEV
    {
        get { return this.description__DEV; }
        set
        {
            if (this.description__DEV != value)
            {
                this.description__DEV = value;
                this.isDirty = true;
            }
        }
    }
    public System.Int32 FK_EnumArray_ID_DataExportType
    {
        get { return this.fK_EnumArray_ID_DataExportType; }
        set
        {
            if (this.fK_EnumArray_ID_DataExportType != value)
            {
                this.fK_EnumArray_ID_DataExportType = value;
                this.isDirty = true;
            }
        }
    }
    public System.Boolean IsDeleted
    {
        get { return this.isDeleted; }
        set
        {
            if (this.isDeleted != value)
            {
                this.isDeleted = value;
                this.isDirty = true;
            }
        }
    }
    #endregion