New Message on dotNET User Group Hyd

Varad's .NET Info

Reply
  Reply to Sender   Recommend Message 41 in Discussion
From: Varad_RS

Getting column names from SQLDataReader
--------------------------------------------------------------

There are many methods to get data out of it, but they all need an index of
the column... how to get the particular index? how to get each value out of
the SqlDataReader?

The SqlDataReader has a number of methods to get the column data, including
GetString(), GetDateTime(), GetValue(), etc. Each of these methods takes an
Int32 value as an input - this is the column index value. The index expected
is a 0-based index of the desired column.

For example, with a SQL statement like:

SELECT Name, Age, Salary FROM MyTable

Name is index 0; Age is index 1; Salary is index 2.

String _name = MyReader.GetString(0);
Int32 _age = MyReader.GetInt32(1);
Decimal _salary = MyReader.GetDecimal(2);

You can also use:

[C#]

MyReader["Name"].ToSting();

[VB]

MyReader("Name").ToString()

...if you want to use the column names.

To loop through the columns you can use a method like this:

[C#]

Int32 i;
while ( myReader.Read() )
{
for ( i=0; i < myReader.FieldCount; i++ )
{
Response.Write ( myReader.GetValue(i).ToString() + "<br>");
}
}

[VB]

Dim i As Int32
While myReader.Read()
For i=0 to myReader.FieldCount-1
Response.Write ( myReader.GetValue(i).ToString() & "<br>");
Next
End While


Regards,
Varad
http://weblogs.asp.net/Varad
"We Learn Together & We Grow Together"

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE!
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/


View other groups in this category.

Click Here
Also on MSN:
Start Chatting | Listen to Music | House & Home | Try Online Dating | Daily Horoscopes

To stop getting this e-mail, or change how often it arrives, go to your E-mail Settings.

Need help? If you've forgotten your password, please go to Passport Member Services.
For other questions or feedback, go to our Contact Us page.

If you do not want to receive future e-mail from this MSN group, or if you received this message by mistake, please click the "Remove" link below. On the pre-addressed e-mail message that opens, simply click "Send". Your e-mail address will be deleted from this group's mailing list.
Remove my e-mail address from dotNET User Group Hyd.

Reply via email to