Re: Filter Property with ADO, VB6 and SQL extremely slow



Jim Oliver wrote:
Hello all,

I have found many references to this issue but no answer. I have a
table with approximately 1,000,000 records. I need to find 1 record
by the primary key. I have tried many things, but the only thing
that seems to work is ADO's filter property.

Huh??? How about using a WHERE clause in the SQL statement used to open the
recordset ???


The .Find property won't work because the primary key has more than
one field.

The .Seek property isn't supported under SQL Server.

But the .Filter property takes an enourmous amount of time (I don't
know how much yet, as it has not yet returned the record.)

Well ... duhhh! :-)


The code is basically this:

CON.Open "Provider=SQLOLEDB.1;Server=TheServer;Database=TheDatabase"
RS.Open TableName, CON, nRSCursorMode, nRSLockMode, adCmdTableDirect
RS.Filter = "field1='abc' AND field2='def'"

mmmmkayyyy ...


How do I find a record with the primary key using ADO on a SQL Server
database? Surely it shouldn't take hours just to return 1 record.

Oh no ... it definitely shouldn't. But to return 1 million records so yor
recordset object can inspect all of them looking for that one record ....

I
could understand if the fields weren't the primary key, but in this
case the key consists exactly of "field1" and "field2". Shouldn't
SQL Server just look up the values on the key and return the record?

Not when you don't use a sql statement with a where clause ... By using
adCmdTableDirect, you've told it to give you all the records ...


Isn't there some way to simulate the .Seek method in SQL Server?

Well yeah ... that's what a database engine does ... if you tell it to.

If
I can't do it through ADO, can I do it using direct calls to SQL
Server, and if so, how?


My preference is to use stored procedures, but I will bypass that approach
unless you ask me to elaborate in a subsequent message. To do it in your
code, do this:

Dim sql as string
sql = "select <comma-delimited list of fields you wish to retrieve> " & _
"FROM Tablename " & _
"WHERE field1='abc' AND field2='def'"
RS.Open sql, CON,,,adCmdText

If you need to use variables here for the key values, use parameter markers,
like this:
sql = "select <comma-delimited list of fields you wish to retrieve> " & _
"FROM Tablename " & _
"WHERE field1=? AND field2=?"

dim parmval1 as string, parmval2 as string
parmval1="abc"
parmval2="abc"
dim arParms as variant
arParms - Array(parmval1,parmval2)
Dim cmd as new adodb.command
with cmd
set .activeconnection=con
.commandtext=sql
.commandtype = adCmdText
set RS = .Execute(,arParms)
end with

gotta go - the wife's calling me to supper ... :-)

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


.



Relevant Pages

  • Re: SQL Express vs. Jet
    ... > Wieso mit Access? ... Der SQL Server greift auf eine simple *.mdf zu:-) ... Dim dadp1 As New OleDb.OleDbDataAdapter ...
    (microsoft.public.de.german.entwickler.dotnet.datenbank)
  • Re: Adding new records with 3 relationships
    ... You are building the SQL ... A primary key defines a record as a unique entity. ... A City is a city regardless of location. ... Dim MyRS, MyBldg As DAO.Recordset ...
    (microsoft.public.access.queries)
  • Re: How can a datatable be written to a new SQL Server table?
    ... > a SQL Server from where it can be manipulated. ... > created SQL Server table, (this has been apparently successful), ... > Dim blnRVal As Boolean = False ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Vb newb needs help on data import/export
    ... There is a data import thru the SQL Server. ... It is launched form the same SQL Server Management Studio database ... Dim cnConn As New OleDbConnection ...
    (microsoft.public.dotnet.languages.vb)
  • Problem with Response.Redirect
    ... Here data is added to a database in People ... The primary key is Name. ... Dim l_ColorPreference ... 'Response.Write SQL ...
    (microsoft.public.inetserver.asp.db)