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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...