import file in dataset problem

  • Thread starter Thread starter Eric Caron
  • Start date Start date
Hi ERic,
Hi

I try to import file in a dataset. How I can do this ?

First you need a DataTable to put the stuff in. If you want to import a
CSV-file, you can separate the different items using the
String.Split()-method. For example (from scratch):


===============================
// Create dynamic Table
DataTable dt = new DataTable();

// Create Row Data - in this case 3 columns which means
// That in your CSV-file are also only 3 items per row
dt.Columns.Add("ComputerID", System.Type.GetType("System.String"));
dt.Columns.Add("Computername", System.Type.GetType("System.String"));
dt.Columns.Add("Active", System.Type.GetType("System.String"));

// Read the Textfile
StreamReader sr = new StreamReader(yourfilename);

string datStr;
do {
datStr = sr.ReadLine();
if ( datStr != null ) {
string[] rowData = sr.Split(',');

// CreateRow
DataRow dr = dt.NewRow();
// Fill Row
dr["ComputerID"] = rowData[0];
dr["Computername"] = rowData[1];
dr["Active"] = rowData[2];
// Add Row
dt.Rows.Add( dr );
}
} while ( datStr != null );

DataSet ds = new DataSet();
// Insert Table
ds.Tables.Add( dt );

===============================

Remember, this is not proved, I just wrote it down from scratch ... but this
is the way to go, I suppose.

Regards,
 
Back
Top