c# - Storing n x m matrix in string -


this datatable:

| 1 0 1 0 ...| | 0 1 0 1 ...| | 1 0 1 0 ...| | . . . . ...| 

i want store data in database in string form, this:

1010 0101 1010... or 1010;0101;1010... etc... 

also need read datatable.

there matrix library @ c# didnt me. how can it? in advance!

assuming have db structure has textblob in it, datatable types integers being 0 , 1 , no nulls:

store in db datatable called datatable

stringbuilder finalstring = new stringbuilder(); (int = 0; < datatable.rows.count; i++) {     datarow myrow = datatable.rows[i];     foreach(object item in myrow.itemarray)     {         if(item int)         {             finalstring.append(item.tostring());         }         else         {             //error         }     }     finalstring.append(";"); }  //todo write here query store finalstring.tostring() in db 

load db assuming have string db patterntoload:

string[] rows = patterntoload.split(new string[] { ";" }, stringsplitoptions.removeemptyentries); datatable newdt = new datatable(); if(rows.length > 0) {     int numofcolumns = rows[0].length;     //since matrix not sparse rows have same length, create layout     for(int = 0; < numofcolumns; i++)     {         newdt.columns.add(i.tostring(), typeof(int));     }     //now go through rows in string format , fill actual datatable     foreach(string currentrow in rows)     {         datarow newrow = newdt.newrow();         for(int j = 0; j < numofcolumns; j++)         {             newrow[j] = currentrow[j] = '1' ? 1 : 0;         }         newdt.rows.add(newrow);     } } 

you can make lot prettier gives general idea do. have ommitted lot of chaecks should go production code keep compact.


Comments