Jump to content

Functions to create ADO.net Datatable to Unitronics Datatables, and visa versa


rohitigupta

Recommended Posts

This is a concept:

private System.Data.DataTable getDataTableFromTable(Unitronics.DataTables.Table table)
    {
	    System.Data.DataTable result = new DataTable();
	    foreach (var col in table.Columns)
	    {
		    DataColumn column = new DataColumn(col.Name, getTypeOfColumn(col));
		    result.Columns.Add(column);
	    }
	    foreach (var row in table.Rows)
	    {
		    var resultRow = result.NewRow();
		    int colIndex = 0;
		    foreach (var cell in row.Cells)
		    {
			    resultRow[colIndex] = cell.Fields[0];
			    colIndex++;
		    }
	    }
	    return result;
    }
    private void setDataTableValuesIntoTable(System.Data.DataTable dataTable, Unitronics.DataTables.Table table)
    {
	    int rowIndex = 0;
	    foreach (DataRow row in dataTable.Rows)
	    {
		    int cellIndex = 0;
		    foreach (var cell in row.ItemArray)
		    {
			    table.Rows[rowIndex].Cells[cellIndex].Fields[0].Value = cell;
			    cellIndex++;
		    }
		    rowIndex++;
	    }
    }
    private Type getTypeOfColumn(Unitronics.DataTables.Columns.Column col)
    {
	    Type result;
	    switch (col.Type)
		    {
			    case FieldType.Boolean:
				    result = typeof(bool);
				    break;
			    case FieldType.Byte:
				    result = typeof(byte);
				    break;
			    case FieldType.Int16:
				    result = typeof(short);
				    break;
			    case FieldType.Int32:
				    result = typeof(int);
				    break;
			    case FieldType.UInt16:
				    result = typeof(ushort);
				    break;
			    case FieldType.UInt32:
				    result = typeof(uint);
				    break;
			    case FieldType.Float:
				    result = typeof(float);
				    break;
			    case FieldType.String:
				    result = typeof(string);
				    break;
			    case FieldType.Timer:
				    result = typeof(TimeSpan);
				    break;
			    default:
				    throw new NotImplementedException();
		    }
	    return result;
    }

I didn't test it...

Basically, one function returns a DataTable from the Unitronics Data Table, and the other one fills a Unitronics DataTable from a Data Table (This is because we don't let you create a Unitronics Data Table, and you shouldn't).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

This site uses cookies. By clicking I accept, you agree to their use.