Web Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsWeb DesignWeb Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 18th, 2012, 08:40 AM
utachan utachan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 2 utachan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 44 m 54 sec
Reputation Power: 0
How can I get datareader values?

What I want is 1 method to b able to connect to the db with a query sent by another method (so I dunno how many fields or lines I'd get)
and return the result.

Any idea how I can do that?
In asp c# .net

Is there a way to save and load objects which I dunno what they are?

I thoguht of making a string which is a connection of all teh fields and then on the method itself split them and save by another parameter holding the sql so it knwos what to execute.

But I find difficulties in actually taking this idea farther if it is possible.

Is it reasonable to have a different method to connect to teh db with each entity needing connection?

Reply With Quote
  #2  
Old August 29th, 2012, 03:18 AM
roars1111 roars1111 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 4 roars1111 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 48 sec
Reputation Power: 0
There are several ways to retrieve values from a DataReader.

The first method is by using the index and looping through the fields in order as retrieved within the Select statement. Starting with the 0 index, you may use the GetValue() Function.

The following code snippets are in VB. A C# snippet is provided at the bottom.

reader = objCommand.ExecuteReader()
While reader.Read()

If Not reader.GetValue(0) Is DBNull.Value Then _
lastname = reader.GetValue(0)
If Not reader.GetValue(1) Is DBNull.Value Then _
firstname = reader.GetValue(1)

End While
reader.Close()
However, this makes the code harder to read, and if a field is added or removed from the query, it could break your code. So let's look at how to retrieve the values by field names.

If your reader is using an OleDbDataReader, we use the reader.Item function.

If Not reader.Item("lastname") Is DBNull.Value Then _
lastname = reader.Item("lastname")
If Not reader.Item("firstname") Is DBNull.Value Then _
firstname = reader.Item("firstname")
If your reader is using a SqlDataReader, we use the reader.GetItem function.

If Not reader.GetItem("lastname") Is DBNull.Value Then _
lastname = reader.GetItem("lastname")
If Not reader.GetItem("firstname") Is DBNull.Value Then _
firstname = reader.GetItem("firstname")
Here is a C# example:

if (reader["lastname"] != DBNull.Value)
lastname = reader["lastname"].ToString();
A reader of my column, Zac, suggests that reader("lastname") instead of reader.Item("lastname") will also work.

He is correct. For the DataReader class implementation, Item is the default property and may be left out. You may access an indexer by using the () operator on the base class object or you may access an indexer by using the Item property.

Another reader of my column, Marneus, pointed out the GetOrdinal option as well. He said "there is a performance hit, each time you use the reader("lastname"), it has to check the meta datas to get the column number."

Readers may refer to this document on the GetOrdinal()

Please note this comment on efficiency:

"Because ordinal-based lookups are more efficient than named lookups, it is inefficient to call GetOrdinal within a loop. Save time by calling GetOrdinal once and assigning the results to an integer variable for use within the loop."

From the article, here is the usage:

' Call GetOrdinal and assign value to variable.
Dim customerID As Integer = reader.GetOrdinal("CustomerID")

' Use variable with GetString inside of loop.
While reader.Read()
Console.WriteLine("CustomerID={0}", reader.GetString(customerID))
End While
Another reader, AndrewSeven, pointed out the cost for hitting the reader twice for each field. He suggests:

object val = reader["lastname"];
if (val != DBNull.Value)
{
lastname = val;
}
And also comments "If your concern is readability, you should probably encapsulate the null test so you can write:

lastname = GetValueFromReader(reader,"lastname");"
Travis responded with this suggestion: "Just do this, it handles nulls nicely unlike .ToString():"

lastname = Convert.ToString(reader["lastname"]);
So I tested his theory by creating columns that would not allow null values, and imported columns with null values. In VB it is written like so:

lastname = Convert.ToString(reader("lastname"))
firstname = Convert.ToString(reader("firstname"))
That is much cleaner! Thanks everyone for your great input!

May your dreams be in ASP.net!

Reply With Quote
  #3  
Old October 1st, 2015, 07:02 AM
ChattanoogaWeb ChattanoogaWeb is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2015
Posts: 2 ChattanoogaWeb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 12 sec
Reputation Power: 0
private static void TestGetValues(SqlDataReader reader)
{
// Given a SqlDataReader, use the GetValues
// method to retrieve a full row of data.
// Test the GetValues method, passing in an array large
// enough for all the columns.
Object[] values = new Object[reader.FieldCount];
int fieldCount = reader.GetValues(values);

Console.WriteLine("reader.GetValues retrieved {0} columns.",
fieldCount);
for (int i = 0; i < fieldCount; i++)
Console.WriteLine(values[i]);

Console.WriteLine();

// Now repeat, using an array that may contain a different
// number of columns than the original data. This should work correctly,
// whether the size of the array is larger or smaller than
// the number of columns.

// Attempt to retrieve three columns of data.
values = new Object[3];
fieldCount = reader.GetValues(values);
Console.WriteLine("reader.GetValues retrieved {0} columns.",
fieldCount);
for (int i = 0; i < fieldCount; i++)
Console.WriteLine(values[i]);
}

Reply With Quote
  #4  
Old October 16th, 2015, 08:41 AM
clapcreative clapcreative is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2015
Location: Los Angeles
Posts: 2 clapcreative User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 41 sec
Reputation Power: 0
Facebook
Get data values

I use SqlDataReader.GetValue method to read values from DB:
Code:
Log.WriteLine("Value of CompanyName column:" + thisReader.GetValue(1)); 


As parameter GetValue get index of column. How could I specify Column Name instead index?

Reply With Quote
  #5  
Old January 7th, 2016, 07:33 AM
addonsolutions addonsolutions is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2016
Posts: 6 addonsolutions User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 56 m 6 sec
Reputation Power: 0
Send a message via Skype to addonsolutions
Facebook
Retrieving data using a DataReader involves creating an instance of the Command object and then creating a DataReader by calling Command.ExecuteReader to retrieve rows from a data source. The following example illustrates using a DataReader where reader represents a valid DataReader and command represents a valid Command object.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsWeb DesignWeb Development > How can I get datareader values?


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.

© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap