data encapsulation

Ask general questions here.
omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

data encapsulation

Post by omayer » Wed May 23, 2012 10:02 pm

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,
Tipu

User avatar
sdaly
Posts: 238
Joined: Mon May 10, 2010 11:04 am
Location: Dundee, Scotland

Re: data encapsulation

Post by sdaly » Thu May 24, 2012 8:12 am

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

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: data encapsulation

Post by omayer » Thu May 24, 2012 9:55 pm

Thank you so much for your help, learning the datatable in progress.
thanks,
Omayer
Tipu