Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 :

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

Posted

right now i'm using a dumb method that deletes the extra columns, but i'm sure that there are better ways

 

		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);
	}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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...