Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 16041

Add item to database...

$
0
0
Hey people. I've got an on-line SQL database. I want to store cell numbers. But actually just for security I'm hashing the cell numbers first and only uploading the hashed values. Which is neither here nor there.

Basically I need to search the SQL database, see if a data item exists, upload it if it's not there and then move on to the next value in the array.

The code below works but it's really slow. Do I need to keep opening and closing a record set just to search the database?

I'm a total SQL rookie and I'm a bit lost. I know there has to be an easier way.

Any help much appreciated. Thanks in advance!

Code:

  Dim MyCellNumbersSQL As New ADODB.Recordset
  MyCellNumbersSQL.CursorLocation = adUseClient
  MyCellNumbersSQL.Index = "idx_CellNumber"

  'AptSQLcon2 is an already open connection to the database

  For T = 1 to 1000
      Hashed$ = MyArray$(T) ' this array contains the pre-calculated hashed values of 1000 cell numbers to add
      Gosub AddMe
  Next T
  MsgBox "Added: " & Str(CTAdded)
  End

AddMe:
  mysearch$ = "Select * from dbo.CellNumbers where CellNumber = '" & Hashed$ & "'"
  MyCellNumbersSQL.Open mysearch$, AptSQLcon2, adOpenKeyset, adLockBatchOptimistic, adCmdText
  If MyCellNumbersSQL.RecordCount = 0 Then
      ' The hash can't be found - add it to the database
      MyCellNumbersSQL.AddNew
      MyCellNumbersSQL("CellNumber") = Hashed$
      MyCellNumbersSQL.Update
      CTAdded = CTAdded + 1
  End If
  MyCellNumbersSQL.Close
  Return


Viewing all articles
Browse latest Browse all 16041

Trending Articles