Showing posts with label exsits. Show all posts
Showing posts with label exsits. Show all posts

Tuesday, March 20, 2012

Question on reading from SQL SERVER

ok I use a datareader..Perhaps not the best method..but...if a better one exsits please let me know...
The problem...
I get info on a user...It returns an unknown amount of rows back...lets say two for instance...

ok...
How do I tell the number of rows...also the ability to reference each row and items in that row...
..
i.e. Row contains this

uname | password | address | Phone | notes

Like I said could contain multiple rows
I need to figure out info for specific rows and items in it...
in datareader I know you can reference each item which I guess you could use x number of variables to get each item in the row but that is tedious...
IS there a better answer??Use a DataAdapter/DataTable then.|||Perhaps you have a code example of how this might be attained|||Yes I do. And so does the rest of the sites google links to when you search those two.


DataTable oDataTable = new DataTable;
SqlDataAdapter oDataAdapter = new SqlDataAdapter(YourCommandObject);
oDataAdapter.Fill(oDataTable);

oDataAdapter.Dispose();
// close your objects

for (int i=0;i<oDataTable.Rows.Count;i++) {
for (int j=0;j<oDataTable.Columns.Count;j++) {
Console.WriteLine(oDataTable.Columns[j].ColumnName + " = " + oDataTable.Rows[i][j].ToString());
}
}

oDataTable.Dispose();