ado.net - find record

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Ok, I have the following code. Does any one know how I go about taking this
record set and trying to find if a record exits in it? - Thanks Dale

Imports System.Data.SqlClient

Public Module modLoadDefaults

Public gCONN As New SqlConnection
Public gbMakeConnection As Boolean
Public rsUsers As New Data.DataTable("Users")

Private Function MakeConnection() As SqlConnection

If gCONN.State <> ConnectionState.Open Then
With gCONN
.ConnectionString = "Integrated Security=TRUE;Initial
Catalog=SQL_Test;Data Source=SERVER\SQLtest "
End With
End If

gbMakeConnection = True

Return gCONN

End Function

Public Function GetUsers() As DataTable

Dim connection As SqlConnection = MakeConnection()
Dim da As New SqlDataAdapter("SELECT * FROM USERS", gCONN)
If gbMakeConnection Then
gCONN.Open()
da.Fill(rsUsers)
If connection.State <> ConnectionState.Closed Then
connection.Close()
Return rsUsers
End If

End Function
 
follow the heirarchy, dataset has tables, tables.count.
tables has rows. rows.count ...........
 
So what rsUsers.DataSet.????

In your code, rsUsers is a DataTable. Did you look at the member functions
of the DataTable class in the documentation? If you had, you would have
seen the Select method which returns an array of DataRows based on criteria
that you specify.

So you could use code similar to this:

Dim strExpression As String = "id > 6" 'Or whatever criteria you need
Dim strSort As String = "name DESC" 'Or whatever sort order you need

'This creates an array of rows that meet the expression "id > 6"
Dim selectedRows as DataRow() = rsUsers.Select(strExpression, strSort)

'This selects ALL rows
Dim allRows As DataRow() = rsUsers.Select()


Check out the docs on DataSet, DataTable, and DataRow. There is a lot of
useful information there.


--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
I second that you can also create a dataview for this table and set the row
filter to the expression.

Chris Dunaway" <"dunawayc[[at]_lunchmeat said:
So what rsUsers.DataSet.????

In your code, rsUsers is a DataTable. Did you look at the member functions
of the DataTable class in the documentation? If you had, you would have
seen the Select method which returns an array of DataRows based on criteria
that you specify.

So you could use code similar to this:

Dim strExpression As String = "id > 6" 'Or whatever criteria you need
Dim strSort As String = "name DESC" 'Or whatever sort order you need

'This creates an array of rows that meet the expression "id > 6"
Dim selectedRows as DataRow() = rsUsers.Select(strExpression, strSort)

'This selects ALL rows
Dim allRows As DataRow() = rsUsers.Select()


Check out the docs on DataSet, DataTable, and DataRow. There is a lot of
useful information there.


--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
Back
Top