G
Guest
I need to have a call into a database which runs a bit of SQL- you know
"Select * from users where Username like %rick%" that sort of thing. I am
just going to go through the one set of rows that comes back- I don't need
all the attributes of a full DataSet which is, as microsoft puts it, an in
memory snapshot of a database.
Is there any reason NOT to just pass around a DataTable object or a
DataRowCollection object?
Specific example; I'll have a class full of methods, in my DAL class. The
second chunk is how to use the function. How does this look to you all?
Public Shared Function GetDataRowCollection(ByVal strSQL As String) As
DataRowCollection
Dim con As SqlConnection = New SqlConnection(DBAccess.SQL_CONN_STR)
Dim sqlDA As SqlDataAdapter
Dim resultDS As DataSet
Dim retRows As DataRowCollection 'This is what we will be handing
back.
Try
con.Open()
sqlDA = New SqlDataAdapter ' Create a new SQLDataAdapter object
resultDS = New DataSet ' Create a new DataSet object
sqlDA.SelectCommand = New SqlCommand(strSQL, con) ' Add a
SelectCommand object
sqlDA.SelectCommand.CommandType = CommandType.Text 'Specify the
Select Command type; Just some SQL
sqlDA.Fill(resultDS, "tmpTblName") ' Populate the DataSet with
the returned data. Make up a table name.
retRows = resultDS.Tables("tmpTblName").Rows ' Retrieve rows
from the DataSet. This should be a copy
Catch ex As Exception
Throw ex
Finally
sqlDA.Dispose()
con.Close()
End Try
Return retRows
End Function
Use it with this:
Dim oRows As DataRowCollection = DBAccess.GetDataRowCollection("select *
from orders")
Dim i As Integer
For i = 0 To oRows.Count - 1
Dim oFocus As New Focus
oRows(i).Item("FocusID")
oRows(i).Item("FocusDescription").ToString()
someCollection.Add(oRows)
Next
"Select * from users where Username like %rick%" that sort of thing. I am
just going to go through the one set of rows that comes back- I don't need
all the attributes of a full DataSet which is, as microsoft puts it, an in
memory snapshot of a database.
Is there any reason NOT to just pass around a DataTable object or a
DataRowCollection object?
Specific example; I'll have a class full of methods, in my DAL class. The
second chunk is how to use the function. How does this look to you all?
Public Shared Function GetDataRowCollection(ByVal strSQL As String) As
DataRowCollection
Dim con As SqlConnection = New SqlConnection(DBAccess.SQL_CONN_STR)
Dim sqlDA As SqlDataAdapter
Dim resultDS As DataSet
Dim retRows As DataRowCollection 'This is what we will be handing
back.
Try
con.Open()
sqlDA = New SqlDataAdapter ' Create a new SQLDataAdapter object
resultDS = New DataSet ' Create a new DataSet object
sqlDA.SelectCommand = New SqlCommand(strSQL, con) ' Add a
SelectCommand object
sqlDA.SelectCommand.CommandType = CommandType.Text 'Specify the
Select Command type; Just some SQL
sqlDA.Fill(resultDS, "tmpTblName") ' Populate the DataSet with
the returned data. Make up a table name.
retRows = resultDS.Tables("tmpTblName").Rows ' Retrieve rows
from the DataSet. This should be a copy
Catch ex As Exception
Throw ex
Finally
sqlDA.Dispose()
con.Close()
End Try
Return retRows
End Function
Use it with this:
Dim oRows As DataRowCollection = DBAccess.GetDataRowCollection("select *
from orders")
Dim i As Integer
For i = 0 To oRows.Count - 1
Dim oFocus As New Focus
oRows(i).Item("FocusID")
oRows(i).Item("FocusDescription").ToString()
someCollection.Add(oRows)
Next