Page 1 of 1

data encapsulation

Posted: Wed May 23, 2012 10:02 pm
by omayer
user code module - currently I am retreiving data from sql server and passing as parameter to the method, but i can't figure out how to separate query and entering data to the object. Thank you in advance.


while(myReader.Read())
{
_scenarioID = myReader["scenarioid"].ToString(); column
_scenarioDescr = myReader["descr"].ToString();

enterID(_scenarioID);
enterScenaDes( _scenarioDescr );
}

--------how will i hold the data after query , then process outside the while loop,

Re: data encapsulation

Posted: Thu May 24, 2012 8:12 am
by sdaly
Load the reader into a datatable - that will allow you to close the connection to the DB and read data whenever/however you want.


Code: Select all

DataTable dt = new DataTable();
myReader = command.ExecuteReader();
dt.Load(myReader);
To check this works, you can add the following extension method to your framework and then execute dt.OutputDatatable-

Code: Select all

public static void OutputDatatable(this DataTable dt){
			int index = 0;
			foreach (DataRow row in dt.Rows) {
				Console.Write("Row " + index.ToString() + "= ");
				foreach (DataColumn col in dt.Columns) {
					string val = row[col].ToString();
					if(string.IsNullOrEmpty(val)){
						val = "NULL";
					}
					Console.Write(val + " , ");
				}
				Console.Write("\n");
				index++;
			}
		}

Re: data encapsulation

Posted: Thu May 24, 2012 9:55 pm
by omayer
Thank you so much for your help, learning the datatable in progress.
thanks,
Omayer