Ci sono riuscito, dopo averci sbattuto un po la testa (l-avrei risolto molto prima se non fosse perche ho abbandonato asp da una vita)
Lo riporto perche sono sicuro che potra essere ultilissimo per altre persone. Sostanzialmente servono tre sub/functions
- la prima crea una copia clonata del recordset originale, ma di tipo oggetto (non di riferimento come con il metodo Clone del recordset)
- la seconda aggiunge un campo di nome "Select" al recordset clonato, che servira come vedrete piu avanti alla funzione Distinct vera e propria
- la terza e quella che restituisce un nuovo recordset con soli valori unici sulla colonna specificata
codice:
Sub AppendField(Recordset, newFieldName, newFieldType, defaultValue)
'========== List of data field types (ADO Const name, code, description)
'adArray 0x2000 Combine with another data type to indicate that the other data type is an array
'adBigInt 20 8-byte signed integer
'adBinary 128 Binary
'adBoolean 11 True or false Boolean
'adBSTR 8 Null-terminated character string
'adChapter 136 4-byte chapter value for a child recordset
'adChar 129 String
'adCurrency 6 Currency format
'adDate 7 Number of days since 12/30/1899
'adDBDate 133 YYYYMMDD date format
'adDBTime 134 HHMMSS time format
'adDBTimeStamp 135 YYYYMMDDHHMMSS date/time format
'adDecimal 14 Number with fixed precision and scale
'adDouble 5 Double precision floating-point
'adEmpty 0 no value
'adError 10 32-bit error code
'adFileTime 64 Number of 100-nanosecond intervals since 1/1/1601
'adGUID 72 Globally unique identifier
'adIDispatch 9 Currently not supported by ADO
'adInteger 3 4-byte signed integer
'adIUnknown 13 Currently not supported by ADO
'adLongVarBinary 205 Long binary value
'adLongVarChar 201 Long string value
'adLongVarWChar 203 Long Null-terminates string value
'adNumeric 131 Number with fixed precision and scale
'adPropVariant 138 PROPVARIANT automation
'adSingle 4 Single-precision floating-point value
'adSmallInt 2 2-byte signed integer
'adTinyInt 16 1-byte signed integer
'adUnsignedBigInt 21 8-byte unsigned integer
'adUnsignedInt 19 4-byte unsigned integer
'adUnsignedSmallInt 18 2-byte unsigned integer
'adUnsignedTinyInt 17 1-byte unsigned integer
'adUserDefined 132 User-defined variable
'adVarBinary 204 Binary value
'adVarChar 200 String
'adVariant 12 Automation variant
'adVarNumeric 139 Variable width exact numeric with signed scale
'adVarWChar 202 Null-terminated Unicode character string
'adWChar 130 Null-terminated Unicode character string
Dim newRecordset, Field, totRecords, Record
totRecords = Recordset.RecordCount
Set newRecordset = Server.CreateObject("ADODB.Recordset")
With newRecordset
.CursorLocation = adUseClient
For Each Field In Recordset.Fields
.Fields.Append Field.Name,Field.Type,Field.DefinedSize,Field.Attributes
Next
.Fields.Append newFieldName, newFieldType
.Open
.ActiveConnection = Recordset.ActiveConnection
If totRecords > 0 Then
Recordset.MoveFirst
For Record = 1 to totRecords
.AddNew
.Fields(newFieldName).Value = defaultValue
For Each Field in Recordset.Fields
.Fields(Field.Name).Value = Field.Value
Next
Recordset.MoveNext
Next
End If
End With
Set Recordset = newRecordset
End Sub
Function CloneRecordset(ByRef fromRecordset)
'Returns a clone of fromRecordset as object, not as a reference like with .Clone method does
Dim rsClone, Stream
Set Stream = Server.CreateObject("ADODB.Stream")
fromRecordset.Save Stream
Set rsClone = Server.CreateObject("ADODB.Recordset")
rsClone.Open Stream ,,3,4
Set CloneRecordset = rsClone
CloneRecordset.ActiveConnection = fromRecordset.ActiveConnection
Set rsClone = Nothing
Stream.Close
Set Stream = Nothing
End Function
Function SelectDistinct(fromRecordset, DistinctFieldName)
Dim toRecordset, totRecords, Record, lastValue
Set toRecordset = CloneRecordset(fromRecordset)
AppendField toRecordset, "Selected", adBoolean, False
toRecordset.Sort = DistinctFieldName
With toRecordset
totRecords = .RecordCount
If totRecords > 0 Then
lastValue = vbNull
.MoveFirst
For Record = 1 To totRecords
If .Fields(DistinctFieldName).Value <> lastValue Then
.Fields("Selected").Value = True
lastValue = .Fields(DistinctFieldName).Value
End If
.MoveNext
Next
toRecordset.Filter = "Selected = True"
End If
End With
Set SelectDistinct = toRecordset
End Function
Sub ShowRecordset(Recordset)
Dim whatever, totRecords
totRecords = Recordset.RecordCount
If totRecords > 0 Then Recordset.MoveFirst
Response.Write "<table border='1'><tr>"
For Each whatever in Recordset.fields
Response.Write "<td>" & whatever.Name & "</TD>"
next
Response.Write "</tr><tr><td>"
If totRecords > 0 Then Response.Write Recordset.GetString(,, "</td><td>", "</td></tr><TR><TD>", "-null-")
Response.Write "</td></tr></table>"
End Sub
Per provare il sistema, basta scrivere
codice:
Set rsDistinct = SelectDistinct(myRs, "NomeColonna")
Call ShowRecordset(rsDistinct)
ShowRecordset non fa altro che emulare il datagrid di .net per vedere il contenuto del nuovo recordset.
L-ho sto gia usando con remote data service e sembra funzionare benissimo