GetRows Equivalent

  • Thread starter Thread starter Leanne
  • Start date Start date
L

Leanne

I have a VB6 project that I'm migrating to VB 2008. There is a routine in the
old project to LoadArrays. By passing the proc name and arrayName to load I
can set up a series of calls at application load and have these arrays
available throughout the application (Info is used in many areas to populated
controls) The old routine makes use of Getrows. Example: arraydef =
rs1.GetRows I am not finding an equivalent in 2008. I have written new code
beload to load data into a dictionary. I have approximately 5 more to do. I
would think there would be a way that I could write a single routine to
handle all 5 but I'm not if this is possible in this version and not sure how
to approach. The dicitionary defs will not be the same for all of them (of
String, Int16). Any help would be appreciated. I'm just learning and have
spent a lot of time just getting this far. Thanks in advance.

Public Sub LoadAPCodeArrayList(ByVal strsqlcmd As String, ByVal APCodeList
As Generic.Dictionary(Of String, Int16))
Dim cn As SqlConnection
Dim cmd As SqlCommand
Dim msqlDataReader As SqlDataReader
Dim i As Integer

'Establish connection
cn = New SqlConnection()
cn.ConnectionString = GetCnnString()
cn.Open()

'Set up sqlcommand object
cmd = New SqlCommand(strsqlcmd, cn)

'Execute command
msqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult)

If msqlDataReader.HasRows Then
With msqlDataReader
While .Read()
APCodeList.Add(msqlDataReader.Item(0).ToString,
CShort(msqlDataReader.Item(1)))
End While
End With
End If

If Not IsNothing(msqlDataReader) Then
msqlDataReader.Close()
msqlDataReader = Nothing
End If

cn.Close()
End Sub
'--Call from form
strSQL = "select apcodes, domintl from AP_Codes"
LoadAPCodeArrayList(strSQL, APCodeList)
 
Leanne,

As it is about datacontrols, then Linq to SQL or a DataTable is much easier
to use.
A lot can be done by both using the designer.

With a datatable it can easily be done all in code too.

I can simple change your code what I did in this message, so watch little
mistakes.

public dt as new DataTable
\\\\
Public Sub FillMyDataTable(ByVal strsqlcmd As String)
Dim cn As new SqlConnection(GetCnnString)
dim da as New SqlDataAdapter(strsqlcmd, cn)
try
da.fill(dt)
catch ex as Exception
messagebox.show(ex.ToString)
end try
end sub
////

If you want to use the datareader, then you have to get everycolumn
seperately but keep in mind to do it then not by the indexnumber but by
name.
By indexnumber gives mostly troubles as you make the littlest change in your
SQL transact code

Cor
 
Back
Top