Convert Object to DataTable System.Data

since I'm the last step from completing my task, I'm a little stuck.

the task is to create a solution for the whole environment to populate any extracted DataTablefrom Sql Db (without using .NET ORM or any ready-made solution)

the structure is the base, as it could be, a class that represents code C#that will correspond to the SQL Server table, and the corresponding method that will populate the extracted table.

This should be a simple task, if I knew correctly how to inherit correctly, what I learn from this implementation.

so thanks for taking the time to try and help solve this problem!

what i have done so far is almost complete:

  • DbTblmetaM is a type of installation containing key information for obtaining the Db table

  • ColumnDefA is a column definition that is the base of the table

and last

  • ColA is the model of the base column (intCol, strCol, dateCol, etc.)

the method is as follows:

public static DataTable ToDataTableTest(this DbSchema.SqlServerTables.ColumnDefA Self)
{
                // v--returns a cloned schema DataTable
    var RtDtt = GetSchemaViaDa(Self.CdefTable.Catalog, Self.CdefTable.Name);

     //use an easy expensive reflection ? 
     //or a the unknown For-loop on each TableObject.ColumnObject.itemsList "Vals"
    return;
}

and the problem I am facing is that I couldn’t figure out how I should structure the Base class object, so the method would simply scroll through its column object to populate any given server table.

*, given the fact that each object is correctly configured so that it matches the structure of its server table.

What am I missing here?

UPDATE

core of my problem:

var someColumn1 = (Self as CdfHddFoldersFiles).FileId;

in the method described above, this is the only way by which I can now access any columns of the server table (the "Table-Objects" field).

, cdfSomeOthertable:ColumnDefA, , .

.

//the setting type contains the key info to fetch Db Table
public class DbTablemetaM
{
    public string Catalog { get; private set; }
    public string Name { get; private set; }
    public virtual string IdenttCol { get; private set; }
    public DbTablemetaM(string bsCatalog, string bsName, string bsIdentCol)
    {
        this.Catalog = bsCatalog;
        this.Name = bsName;
        this.IdenttCol= bsIdentCol;
    }
}
public class ColumnDefA
{
    public virtual DbTablemetaM CdefTable { get; set; }
}
public class CdfHddFoldersFiles : ColumnDefA
{
    public override DbTablemetaM CdefTable
    {
        get
        {
            return base.CdefTable;
        }
        set
        {
            base.CdefTable = value;
        }
    }
    public ColsI.IntC FileId, FileSize;
    public ColsI.StrC CurFileName, DriveL, FilePath, FileExt;

    public ColsI.DateC Created;
    public CdfHddFoldersFiles()
    {
    }
}

public class ColA
{
    public virtual Type DataType { get; set; }
    public virtual string Name { get; set; }
    public virtual List<object> Vals { get; set; }
    public ColA()
    {
       this.Vals = new List<object>();
    }
}

public class ColsI
{
    public class StrC : ColA
    {
        public new String DataType;
        public new List<String> Vals { get; set; }
        public override string Name
        {
            get
            {
                return base.Name;
            }
            set
            {
                base.Name = value;
            }
        }
        public StrC()
        {
            this.Vals = new List<String>();
        }
    }

}

:

- , , clone to DataTable, :

DataTable

public override void fetchAndPopulateServerTable()
{
    this.DttToPush = this.Row.ToDataTableTest();

    var RowsFileId = this.Row.FileId.Vals;
    var RowsCurFileName = this.Row.CurFileName.Vals;
    var RowsDriveL = this.Row.DriveL.Vals;
    var RowsFilePath = this.Row.FilePath.Vals;
    for (int i = 0; i < this.Row.FileId.Vals.Count; i++)
    {
        var Nwdr = this.DttToPush.NewRow();
        Nwdr[this.Row.FileId.Name] = RowsFileId[i];
        Nwdr[this.Row.CurFileName.Name] =RowsCurFileName[i];
        Nwdr[this.Row.DriveL.Name] = RowsDriveL[i];
        Nwdr[this.Row.FilePath.Name] = RowsFilePath[i];


        this.DttToPush.Rows.Add(Nwdr);
    }
}
+4

Source: https://habr.com/ru/post/1616933/


All Articles