Results 1 to 5 of 5

Thread: Ado.Net question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382

    Ado.Net question

    Ok I am doing a check to see if a name is in a database or not. Returns true if it is, false if it isn't there..

    Is the following code the best practical way to do this or is there a better way and I'm being clueless?

    VB Code:
    1. Private Function IsAccount(ByVal strSQL As String) As Boolean
    2.  
    3.         Dim oConn As OleDbConnection = New OleDbConnection(sConn)
    4.         Dim cmd As OleDbCommand = New OleDbCommand(strSQL, oConn)
    5.         Dim r As OleDbDataReader = Nothing
    6.         Dim retValue As Boolean = True
    7.  
    8.         Try
    9.             'Open the connection
    10.             oConn.Open()
    11.  
    12.             'Get the reader
    13.             r = cmd.ExecuteReader
    14.  
    15.             'Check to see if this account name exists
    16.             If (r.Read) Then
    17.                 retValue = True
    18.             Else
    19.                 retValue = False
    20.             End If
    21.  
    22.         Catch ex As Exception
    23.             retValue = True
    24.         Finally
    25.             If (Not r Is Nothing) Then r.Close()
    26.             oConn.Close()
    27.         End Try
    28.  
    29.         Return retValue
    30.  
    31.     End Function


    Any insight, critisizm, or general "You should'nt do it that way" or "Here's a better way" comments are all welcomed...

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Where is the SQL statement? And by 'in the database' do you just mean in a specific table?

    I would have the SQL query return a count of the number of times the name is found and then execute the command scalar so just one value has to come across. That should improve performance and network traffic.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382
    Originally posted by Edneeis
    Where is the SQL statement? And by 'in the database' do you just mean in a specific table?

    I would have the SQL query return a count of the number of times the name is found and then execute the command scalar so just one value has to come across. That should improve performance and network traffic.
    Hey Edneeis, the SQL statement is passed through the function itself from another method. Yes it is checking one table in the Access database, it would look like this..

    VB Code:
    1. "SELECT AcctName FROM Accounts WHERE AcctName = 'Edneeis'"

    You're saying to use a scalar() call and just return the count, mind elaborating on that a bit? I know what the scalar() call does but how would you query just a count or return just a count ?

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Use this as the sql statement:
    SELECT Count(AcctName) FROM Accounts WHERE AcctName = 'Edneeis'
    VB Code:
    1. Private Function IsAccount(ByVal strSQL As String) As Boolean
    2.         'where does sConn come from?
    3.         Dim oConn As New OleDb.OleDbConnection(sConn)
    4.         Dim cmd As New OleDb.OleDbCommand(strSQL, oConn)
    5.         Dim cnt As Integer=0
    6.  
    7.         Try
    8.             'Open the connection
    9.             oConn.Open()
    10.             'Get the count
    11.             cnt = CType(cmd.ExecuteScalar, Integer)
    12.         Catch ex As Exception
    13.         Finally
    14.             oConn.Close()
    15.         End Try
    16.  
    17.         'return boolean based on count
    18.         Return (cnt > 0)
    19.     End Function

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382
    Thanks again Edneeis, making my life easier one line at a time

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width