SQL to resultset

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I have an sql statement in a string variable SQLStr and I also have an
oledbconnection Conn with my database. What can I do from here to run the
sql and get a handle to the resultset in my app? I want to be able to go
through the records in the resultset in a loop and read the field values.

Thanks

Regards
 
John said:
Hi

I have an sql statement in a string variable SQLStr and I also have an
oledbconnection Conn with my database. What can I do from here to run
the sql and get a handle to the resultset in my app? I want to be able
to go through the records in the resultset in a loop and read the
field values.

http://msdn2.microsoft.com/en-us/library/haa3afyz.aspx

http://msdn2.microsoft.com/en-us/library/bh8kx08z.aspx


Here's the whole ADO.Net documentation:
http://msdn2.microsoft.com/en-us/library/e80y5yhx.aspx

You'll find many other helpful topics.


Armin
 
Hi Armin

Many thanks. Is it possible to also assign this datareader resultset to a
gridview control? if not, is there another way?

Thanks

Regards
 
John said:
Hi Armin

Many thanks. Is it possible to also assign this datareader resultset
to a gridview control?

I don't know. Probably not because it must have access to all the data
at any time. The DataReader is forward-only and provides access to the
current record only
if not, is there another way?

Yes, the 2nd link.


Armin
 
John,

I think, for your sake, that you should read a book or at least ADO.NET
documentation from help file to learn the basics.
 
Miha, teaching doesn't help the student alone, it helps the teacher as well.
All good teachers know that.

Regards

Miha Markic said:
John,

I think, for your sake, that you should read a book or at least ADO.NET
documentation from help file to learn the basics.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

John said:
Hi

I have an sql statement in a string variable SQLStr and I also have an
oledbconnection Conn with my database. What can I do from here to run the
sql and get a handle to the resultset in my app? I want to be able to go
through the records in the resultset in a loop and read the field values.

Thanks

Regards
 
John,

These newsgroups aren't meant for teaching - instead they are for solving
problems.
IMO you should know the basics at least for your own good and for not asking
really basic questions.
Note that this is my opinion and I am not forcing you to anything.
 
John, as most mentioned here, it would be beneficial to go through
some basic ADO.Net material, this stuff should come to you pretty
quickly.

But in the meantime, here's the basic method to get a dataset filled
with your sql query (and for the experts, please feel free to chime in
with corrections, I can certainly use the input as well):

The following is with System.Data.SqlClient imported:

Imports System.Data.SqlClient << this goes before the class
declaration.

Dim conn1 As New
SqlConnection("server='serverAddr';uid='userId';pwd='yurPass';database=somethingName;")
Dim sqlString As String = "select * from zipCode"
Dim adapter1 As New SqlDataAdapter(sqlString, conn1)
Dim ds1 As New Data.DataSet

adapter1.Fill(ds1)
adapter1.Dispose()

MsgBox(ds1.Tables(0).Rows(0).Item(1))

You'll be able to access the tables, rows and columns of your result
off the dataset.

Also, if you're looking to loop through your result you can get a
count of the number of rows by:

ds1.Tables(0).Rows.Count

This is what I use, it's by no means the only or the best way of
retrieving data, but seems to work ok.

- Joe
 
Back
Top