Fill data according to a schema?

ThienZ

Well-known member
Joined
Feb 10, 2005
Messages
45
Location
Germany
i want to fill data to a strongly typed dataset fom excel.

if i fill this strongly typed dataset with a sheet which has more columns than the typed dataset, these extra columns are put on the end of the typed dataset.

example :
my strongly typed dataset has 3 columns : name, address, id
the excel sheet has 5 columns : name, birthdate, phone, id, email

if i do this :
C#:
OleDbConnection conn = new 
	OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" +
	@"Data Source=" + filename + ";Extended Properties=Excel 8.0;");
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", conn);
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = cmd;
myDataSet mynewds = new myDataSet();
adapter.Fill(mynewds, "mytable");
conn.Close();

mynewds has these columns : nam, address, id, birthdate, phone, email.

how can i fill my strongly typed dataset only with columns, which in my strongly typed dataset exist?

thx
 
right now im using a dumb method that deletes the extra columns, but im sure that there are better ways

C#:
		public static void adjustDataTable(DataTable sampleDT, DataTable toadjustDT) 
		{
			DataTable extracols = toadjustDT.Clone();
			foreach(DataColumn col in sampleDT.Columns) 
				extracols.Columns.Remove(col.ColumnName);
			foreach(DataColumn col in extracols.Columns) 
				toadjustDT.Columns.Remove(col.ColumnName);
		}
 
Back
Top