473,779 Members | 1,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 MyDatabaseTable Class
....
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 2831
"Frank Milverckowitz" <fr************ ****@yahoo.comw rote 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_Line 1 = String.Empty;
string strAddress_Line 2 = String.Empty;

using (SqlDataReader objReader = <some DataReader object>)
{
while (objReader.Read ())
{
strLastName = objReader["LAST_NAME"].ToString();
strAddress_Line 1 = objReader["ADDRESS_LI NE1"].ToString();
strAddress_Line 2 = objReader["ADDRESS_LI NE2"].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.GetOrdin al("LastName") ;
string v = reader.GetStrin g(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 MyDatabaseTable Class
...
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.GetOrdin al("LastName") ;
string v = reader.GetStrin g(i);

.......

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

except I do enums

public enum EmployeeSingleD efaultLayout
{
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.Execu teReader();
if (null!=idr)
{
while(idr.Read( ))
{

int empid = idr.GetInt32( EmployeeSingleD efaultLayout.Em pID ); //
(don't forget to check for DBNULL also, this is just a quick example)
string lname = idr.GetString( EmployeeSingleD efaultLayout.La stName);
//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.comw rote 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 MyDatabaseTable Class
...
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
4613
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, longitude. My method calculates the distance from a given zipcode to all zipcodes by doing the following: 1. select zipcode, latitude, longitude from zipcode_table;
1
4856
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: SqlDataReader rdr = new cmd.ExecuteReader(); if (rdr.HasRows) { // do something
3
8017
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)); s=String.Format("{0}",myReader.GetString(3)); // null field s=String.Format("{0}",myReader.GetString(4)); One solution is to use:
1
1445
by: DaveF | last post by:
Is there an equivalent String = recordset.GetString(StringFormat, NumRows, ColumnDelimiter, RowDelimiter, NullExpr) -- David
1
1364
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 out dated?
3
404
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
2864
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 reader.Read(), it has a row with 13 data columns, but they're all empty. So, my GetInt() function throws an error. When it jumps to the catch(), reader's columns then show the proper data. What gives? I have reader._data (which is the ID...
13
14598
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 = statement.executeQuery(s2); resultset.next();) { s10 = resultset.getString(1); s11 = resultset.getString(2); s12 = resultset.getString(3); } resultset.close();
3
4200
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 object. It seems I can only move through it sequentially. Is that true? Dom
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10306
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.