473,508 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SqlDataReader accessed only by field ordinals?

I notice that using the SqlDataReader requires the use of ordinal field
references rather than by name.
For example,

do while (myDataReader.Read())
Console.Write(myDataReader.GetInt32(0).ToString() + Chr(9))
Console.Write(myDataReader.GetString(2) + " " +
myDataReader.GetString(1) + Chr(9))
Console.Write(myDataReader.GetString(3) + Chr(9))
if (myDataReader.IsDBNull(4)) then
Console.Write("N/A" + Chr(10))
else
Console.Write(myDataReader.GetInt32(4).ToString() + Chr(10))
end if
loop

Is there a way for me to read data from SQL by field name (ie: column name)
rather than by ordinal? It is much easier to program this way and also
handles situations where fields have been inserted or otherwise changed
position in a table.

Thanks in advance,

Tom

Nov 21 '05 #1
5 2206
It works fine with field names. You just need to do a cast (CType for VB) to
the correct type, since you get Object back.

Tom Dacon
Dacon Software Consulting

"Tom Edelbrok" <an*******@anonymous.com> wrote in message
news:EDY6e.25476$vt1.11985@edtnps90...
I notice that using the SqlDataReader requires the use of ordinal field
references rather than by name.
For example,

do while (myDataReader.Read())
Console.Write(myDataReader.GetInt32(0).ToString() + Chr(9))
Console.Write(myDataReader.GetString(2) + " " +
myDataReader.GetString(1) + Chr(9))
Console.Write(myDataReader.GetString(3) + Chr(9))
if (myDataReader.IsDBNull(4)) then
Console.Write("N/A" + Chr(10))
else
Console.Write(myDataReader.GetInt32(4).ToString() + Chr(10))
end if
loop

Is there a way for me to read data from SQL by field name (ie: column name) rather than by ordinal? It is much easier to program this way and also
handles situations where fields have been inserted or otherwise changed
position in a table.

Thanks in advance,

Tom

Nov 21 '05 #2
Tom,
It works fine with field names. You just need to do a cast (CType for VB)
to
the correct type, since you get Object back.


Casting in VBNet is DirectCast, CType is converting

Cor

Nov 21 '05 #3
Tom,

In addition to the other Tom the link too the item from the reader

http://msdn.microsoft.com/library/de...sitemtopic.asp

\\\
Dim arrImage() As Byte = DirectCast(rdr.Item("Photo"), Byte())
///

I hope this helps,

Cor


Nov 21 '05 #4
Tom,

Looking at your problem, I got this idea for an often asked question for
filling a datable while using a progressbar
\\\Needs a progressbar, a button and a datagrid
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As New SqlClient.SqlConnection("Server=MyServer;" & _
"DataBase=Northwind; Integrated Security=SSPI")
Dim cmd As New SqlClient.SqlCommand("Select Count(*) from
Employees", conn)
conn.Open()
ProgressBar1.Maximum = DirectCast(cmd.ExecuteScalar, Integer)
ProgressBar1.Step = 1
ProgressBar1.Minimum = 0
cmd.CommandText = "SELECT * FROM Employees"
Dim rdr As SqlClient.SqlDataReader = cmd.ExecuteReader()
Dim dt As DataTable
While rdr.Read
If dt Is Nothing Then
dt = New DataTable
Dim dtschema As DataTable
dtschema = rdr.GetSchemaTable
For Each drschema As DataRow In dtschema.Rows
dt.Columns.Add(drschema("ColumnName").ToString, _
Type.GetType(drschema("DataType").ToString))
Next
End If
ProgressBar1.PerformStep()
Dim dr As DataRow = dt.NewRow
Dim tempObject(dt.Columns.Count - 1) As Object
rdr.GetValues(tempObject) 'did not go in one time
dr.ItemArray = tempObject
dt.Rows.Add(dr)
Threading.Thread.Sleep(500) 'only for showing
End While
DataGrid1.DataSource = dt
rdr.Close()
conn.Dispose()
End Sub
///
Maybe it gives you as well some ideas?

Cor

"Tom Edelbrok" <an*******@anonymous.com> schreef in bericht
news:EDY6e.25476$vt1.11985@edtnps90...
I notice that using the SqlDataReader requires the use of ordinal field
references rather than by name.
For example,

do while (myDataReader.Read())
Console.Write(myDataReader.GetInt32(0).ToString() + Chr(9))
Console.Write(myDataReader.GetString(2) + " " +
myDataReader.GetString(1) + Chr(9))
Console.Write(myDataReader.GetString(3) + Chr(9))
if (myDataReader.IsDBNull(4)) then
Console.Write("N/A" + Chr(10))
else
Console.Write(myDataReader.GetInt32(4).ToString() + Chr(10))
end if
loop

Is there a way for me to read data from SQL by field name (ie: column
name) rather than by ordinal? It is much easier to program this way and
also handles situations where fields have been inserted or otherwise
changed position in a table.

Thanks in advance,

Tom

Nov 21 '05 #5
Tom,
| Is there a way for me to read data from SQL by field name (ie: column
name)
| rather than by ordinal?
What I do is get the ordinals just before the loop using the
DataReader.GetOrdinalfunction.

Something like:

myDataReader = myCommand.ExecuteReader(...)

Dim fieldId As Integer = myDataReader.GetOrdinal("Id")
Dim fieldLastName As Integer = myDataReader.GetOrdinal("LastName")
Dim fieldFirstName As Integer = myDataReader.GetOrdinal("FirstName")
Dim fieldAddress As Integer = myDataReader.GetOrdinal("Address")
Dim fieldAmount As Integer = myDataReader.GetOrdinal("Amount")

Do While (myDataReader.Read())
Console.Write(myDataReader.GetInt32(fieldId).ToStr ing() +
Chr(9))
Console.Write(myDataReader.GetString(fieldFirstNam e) + " " +
myDataReader.GetString(fieldLastName) + Chr(9))
Console.Write(myDataReader.GetString(fieldAddress) + Chr(9))
If (myDataReader.IsDBNull(fieldAmount)) Then
Console.Write("N/A" + Chr(10))
Else
Console.Write(myDataReader.GetInt32(fieldAmount).T oString()
+ Chr(10))
End If
Loop

| It is much easier to program this way and also
| handles situations where fields have been inserted or otherwise changed
| position in a table.

When not using GetOrdinal as above, I've seen people use either constants or
Enums instead of integer literals to represent the ordinal positions...

Hope this helps
Jay
"Tom Edelbrok" <an*******@anonymous.com> wrote in message
news:EDY6e.25476$vt1.11985@edtnps90...
|I notice that using the SqlDataReader requires the use of ordinal field
| references rather than by name.
| For example,
|
| do while (myDataReader.Read())
| Console.Write(myDataReader.GetInt32(0).ToString() + Chr(9))
| Console.Write(myDataReader.GetString(2) + " " +
| myDataReader.GetString(1) + Chr(9))
| Console.Write(myDataReader.GetString(3) + Chr(9))
| if (myDataReader.IsDBNull(4)) then
| Console.Write("N/A" + Chr(10))
| else
| Console.Write(myDataReader.GetInt32(4).ToString() + Chr(10))
| end if
| loop
|
| Is there a way for me to read data from SQL by field name (ie: column
name)
| rather than by ordinal? It is much easier to program this way and also
| handles situations where fields have been inserted or otherwise changed
| position in a table.
|
| Thanks in advance,
|
| Tom
|
|
|
Nov 21 '05 #6

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

Similar topics

2
6159
by: SC | last post by:
Hi, After a SqlDataReader.ExecuteReader method has returned, how can I get the field names of every column that were returned? It has to be possible, right? I looked through the different...
5
12115
by: Wing | last post by:
Hi all, I execute a stored procedure in my C# code, assign the result to the SqlDataReader object and display it with datagrid. the question I like to ask, is possible to edit the datagrid...
2
1622
by: Matt | last post by:
I just want to get the value of field UserName and Password in SqlDataReader object dr. But it yeields run-time error "Invalid attempt to read when no data is present" on line dr("UserName") and...
5
6577
by: Woody Splawn | last post by:
I have some code that looks like this: Dim SSN, LName, FName, M As String mySqlConnection = New SqlConnection(myConnectionString) Dim sql_Command As New SqlCommand( _ "Select SSN, LName, FName,...
7
1698
by: Web learner | last post by:
I am trying to create a method GetDataFor(string column) becaues I have to repeat the same statements for several columns but I get an error as follows: The name 'dr' does not exist in the current...
3
3242
by: dchristjohn | last post by:
I am currently developing a small windows application using Visual Basic via Visual Studio 2005. My database resides on a SQL 2000 server. I have a table with three fields: id (int, Not Null)...
12
7225
by: 2b|!2b==? | last post by:
I want to export my C++ classes in a DLL, using ordinal # - rather than by name. Will anyone care to enumerate through the steps required to do this? I am already failiar with exporting classes...
3
1838
by: Dom | last post by:
Still teaching myself about SQLDataReader and associated classes. I have to say, the new HELP screens from MS are just about useless!! Let's assume I have just read some records from a table...
0
7125
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
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,...
1
7049
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7499
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5631
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,...
0
4709
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3199
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.