Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Database and Storing Fields in Variables

Status
Not open for further replies.

AkutaSame

Programmer
Oct 5, 2002
97
0
0
US
As the subject states, I am looking for a way to store database items/records inside variables. I am not pulling one value, but 9 fields and hundreds of rows. Thus, I cannot use ExecuteScalar or the like.

What I am doing is pulling the multiple rows of these 9 fields (there are more fields in the DB, these are just the ones I have filtered out as needed) and want to output the data as a CSV. I have no problem with text manipulation nor binding data to a control on a form. I am writing a function in VB.net for someone else to call from their ASP.net page. In this particular situation, I do not need to retain or modify the data just take the data, manipulate it into CSV format, and output.

So far the whole program is written save for assigning a variable to the field values i.e.:

Dim sFName As String
Dim sLName As String
Dim sUID As String

sFName = <enter code>
sLName = <enter code>
sUID = <enter code>

As I said, I have no problem with the manipulation, nor with binding data fields to controls... but I cannot use controls in this particular situation.

Any suggestions?
 
Forgot to note. <enter code> would be where I would contain the code assigning the field values to the variable (I know all of you are intelligent, so please do not take the update as a &quot;hit to your brains&quot;).
 
Alrighty, since no one answered the question, I have a solution already.
(For those of you who have wondered how to do this):

using &quot;LastName&quot; for an example as a field,

LastName = myDataSet.Tables(&quot;TableName&quot;).Rows(Me.BindingContext(myDataSet, &quot;TableName&quot;).Position).Item(&quot;LastName&quot;)

Enjoy.
 
It's easier than that:

Code:
LastName = myDataSet.Tables(&quot;TableName&quot;).Rows(i).Item(LastName)

i would be a counter variable in which you would iterate through a loop.

But a shortcut to get all the data from each row in one swoop would be something like this:

Code:
Dim CSVRow As String
Dim r As DataRow
     For Each r In ds.Tables(&quot;yourTable&quot;).Rows
          CSVRow = &quot;&quot;
          Dim value As Object
          For Each value In r.ItemArray
                CSVRow += value.ToString & &quot;,&quot;
          Next
          CSVRow = Mid(CSVRow, 1, Len(CSVRow) - 1)
          'Do Something with your Row of Data Here....write it out as a line in your .csv file or something
            
      Next
 
Yes, I know that I could have used a variable there... However, what happens if by some chance the variable's value changes? This way was a way I found with minimal risk of loss of data.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top