473,382 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Framework 2.0 SqlDataReader equivalent to javas ResultSet rs.getString( "ColumnNameHere" ) ?

Hi,

Newbie question about SqlDataReader column value access...

In java jdbc code to get a value from a table column we can pass the column
name (instead of an int index or offset):

String strLastName = rs.getString( "LastName" );
Now, let's say that LastName is the 14th column in the table...
Does this mean that in .NET SqlDataReader my only option is to define my own
equates to map column indexes to string values?

For example, Using the SqlDataReader it looks like my only option is to pass
an index for the column I want the value from:

string strLastName = sdr.GetString( 13 );

or do I need to define my own equates for every table in my database so that
my code is readable?
(e.g.)

class MyDatabaseTableClass
....
public int LAST_NAME = 13;
public int ADDRESS_LINE1 = 14;
public int ADDRESS_LINE2 = 15;

Or am I just not finding the method I am looking for here?

thanks for any tips,

Frank
Jan 19 '07 #1
3 2809
"Frank Milverckowitz" <fr****************@yahoo.comwrote in message
news:45**********************@roadrunner.com...
Or am I just not finding the method I am looking for here?
Yes - I think you're confusing a SqlDataReader with a DataSet. A
SqlDataReader is a forward-only recordset (for want of a better term) and,
more or less, the only thing you can do with it is read each record once
from beginning to end. Therefore, the properties and methods that you think
are missing simply aren't necessary...

string strLastName = String.Empty;
string strAddress_Line1 = String.Empty;
string strAddress_Line2 = String.Empty;

using (SqlDataReader objReader = <some DataReader object>)
{
while (objReader.Read())
{
strLastName = objReader["LAST_NAME"].ToString();
strAddress_Line1 = objReader["ADDRESS_LINE1"].ToString();
strAddress_Line2 = objReader["ADDRESS_LINE2"].ToString();
}
}
Jan 19 '07 #2
the datareader indexer allows the column name or index:

string v = reader["LastName"].ToString();

to use the helpers you can convert the name to index

int i = reader.GetOrdinal("LastName");
string v = reader.GetString(i);

-- bruce (sqlwork.com)
Frank Milverckowitz wrote:
Hi,

Newbie question about SqlDataReader column value access...

In java jdbc code to get a value from a table column we can pass the column
name (instead of an int index or offset):

String strLastName = rs.getString( "LastName" );
Now, let's say that LastName is the 14th column in the table...
Does this mean that in .NET SqlDataReader my only option is to define my own
equates to map column indexes to string values?

For example, Using the SqlDataReader it looks like my only option is to pass
an index for the column I want the value from:

string strLastName = sdr.GetString( 13 );

or do I need to define my own equates for every table in my database so that
my code is readable?
(e.g.)

class MyDatabaseTableClass
...
public int LAST_NAME = 13;
public int ADDRESS_LINE1 = 14;
public int ADDRESS_LINE2 = 15;

Or am I just not finding the method I am looking for here?

thanks for any tips,

Frank

Jan 19 '07 #3

A couple of things.

Remembering that a SqlDataReader is ~an implementation of IDataReader, you
should look at the IDataReader interface.
You can decide with methods/properties/indexers are SqlServerReader specific
and are generic to IDataReader.

(thus you could program against the interface, and have future flexiblity).

......

Having said that, do a google search for "csla SafeDataReader".
this is a class a guy wrote to handle DBNULL, and has encapsulated the
method bruce describes:
int i = reader.GetOrdinal("LastName");
string v = reader.GetString(i);

.......

Having said that, I actually code now to the CONST method you describe.

except I do enums

public enum EmployeeSingleDefaultLayout
{
EmpID = 0 ,
LastName = 1 ,
FirstName = 2 ,
DateOfBirth = 3
}
I do this because I always code against an IDataReader. And when I
serialize my datareader to an object, (or object collection)
I use

IDataReader idr = something.ExecuteReader();
if (null!=idr)
{
while(idr.Read())
{

int empid = idr.GetInt32( EmployeeSingleDefaultLayout.EmpID ); //
(don't forget to check for DBNULL also, this is just a quick example)
string lname = idr.GetString( EmployeeSingleDefaultLayout.LastName);
//the rest I'll save for you

Employee e = new Employee (empid , lname);

}
}

something like that.

the enum is more maintainable and more organized
but I like the generic-ness of IDataReader, because I can get one against
any RDBMS, protecting me.

and you end up writing the same type code for

Access
Sql Server
Oracle
Excel

even a text file.
Anyway, that's my approach.

"Frank Milverckowitz" <fr****************@yahoo.comwrote in message
news:45**********************@roadrunner.com...
Hi,

Newbie question about SqlDataReader column value access...

In java jdbc code to get a value from a table column we can pass the
column
name (instead of an int index or offset):

String strLastName = rs.getString( "LastName" );
Now, let's say that LastName is the 14th column in the table...
Does this mean that in .NET SqlDataReader my only option is to define my
own
equates to map column indexes to string values?

For example, Using the SqlDataReader it looks like my only option is to
pass
an index for the column I want the value from:

string strLastName = sdr.GetString( 13 );

or do I need to define my own equates for every table in my database so
that
my code is readable?
(e.g.)

class MyDatabaseTableClass
...
public int LAST_NAME = 13;
public int ADDRESS_LINE1 = 14;
public int ADDRESS_LINE2 = 15;

Or am I just not finding the method I am looking for here?

thanks for any tips,

Frank


Jan 19 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sulla | last post by:
I ran into an interesting "problem" the other day when I was working on a very simple zip code utility function. I have a table in Oracle with the following columns: zipcode, state, latitude,...
1
by: charliewest | last post by:
Is there a .Net Compact Framework equivalent in the system.data.sqlserverce namespace of the .Net Framework system.data.sqlclient DataReader object's "Has.Rows" method? For example: ...
3
by: GTi | last post by:
I have a SQL database and some fields may be null. This will trow an exeption: string s=new string; s=String.Format("{0}",myReader.GetString(1)); s=String.Format("{0}",myReader.GetString(2));...
1
by: DaveF | last post by:
Is there an equivalent String = recordset.GetString(StringFormat, NumRows, ColumnDelimiter, RowDelimiter, NullExpr) -- David
1
by: Daniel | last post by:
for some reason when i deploy my C# application on windows 2003 occasionaly rdr.ToString() hangs where rdr is a System.Data.SqlClient.SqlDataReader. Is there a fix for this? is my visual studio.net...
3
by: Alan T | last post by:
How do I know how many records in SqlReader ? What I want to do is I would like to assign the field values in SQLReader to an array. eg. int i = 0; while (objReader.Read()) {
13
by: lithoman | last post by:
I'm stumped here. I run the procedure Batch_Select against the database with @ID=18 and I get the expected data. When it loads into a SqlDataReader, it gets messed up somehow. Initially, after the...
13
by: ajos | last post by:
hi frnds, im doing some db related operations in my web project,im using mysql 5.0,i have the following piece of code in my action class- for(resultset =...
3
by: Dom | last post by:
In the old days, using ADO, I used to be able to move up and down in a resultset, but using functions like MoveNext, MoveFirst, MoveLast, MovePrev. I don't see any of that in the SQLDataReader...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.