Brakuje mi możliwości zwrócenia czytelnego zestawu rekordów z funkcji klasycznej ASP.Zestaw rekordów powrotu z funkcji w klasycznej ASP
To co wymyśliłem, ale to nie działa:
Response.Clear
Response.CharSet = "utf-8"
Response.ContentType = "text/plain"
Dim Count
Set Count = Test
Response.Write Count.Fields(0).Value
Function Test
Dim Query, Connection, Command, Recordset
Query = " blah blah blah "
Set Connection = Server.CreateObject("ADODB.Connection")
Set Command = Server.CreateObject("ADODB.Command")
Set Recordset = Server.CreateObject("ADODB.Recordset")
Connection.ConnectionString = "blah blah blah"
Connection.Open
Set Command.ActiveConnection = Connection
Command.CommandText = Query
Set Recordset = Command.Execute
Set Test = Recordset
Recordset.Close
Connection.Close
Set Recordset = Nothing
Set Command = Nothing
Set Connection = Nothing
End Function
Linia Response.Write Count.Fields(0).Value
daje błąd Item cannot be found in the collection corresponding to the requested name or ordinal.
.
Zastąpienie go Response.Write Count.Status
Dostaję błąd Operation is not allowed when the object is closed.
.
Dodanie Count.Open
powoduje błąd The connection cannot be used to perform this operation. It is either closed or invalid in this context.
.
Edit po znak B za odpowiedź:
już spojrzał na odłączonych zestawów rekordów, ale nie wiem, jak ich używać w moim przykładzie: każdy poradnik zasila zapytanie bezpośrednio do rekordów z Recordset.Open
, ale Używając sparametryzowanych zapytań, a nawet próbując na wiele sposobów, nie mogłem uzyskać tego samego wyniku, gdy w ten sposób jest ADODB.Command
.
Co należy zrobić?
Z góry dziękuję.
Oto rozwiązanie oparte na Eduardo Molteni za odpowiedź:
Funkcja który współdziała z bazy danych:
Function Test
Dim Connection, Command, Recordset
Set Connection = Server.CreateObject("ADODB.Connection")
Set Command = Server.CreateObject("ADODB.Command")
Set Recordset = Server.CreateObject("ADODB.Recordset")
Connection.ConnectionString = "blah blah blah"
Connection.Open
Command.ActiveConnection = Connection
Command.CommandText = "blah blah blah"
Recordset.CursorLocation = adUseClient
Recordset.Open Command, , adOpenForwardOnly, adLockReadOnly
Set Recordset.ActiveConnection = Nothing
Set Test = Recordset
Connection.Close
Set Recordset = Nothing
Set Command = Nothing
Set Connection = Nothing
End Function
Kod, który wywołuje funkcję:
Response.Clear
Response.CharSet = "utf-8"
Response.ContentType = "text/plain"
Dim Recordset
Set Recordset = Test
Response.Write Recordset.Fields(0).Value
Recordset.Close
Set Recordset = Nothing
Twój końcowy fragment kodu brakuje 'Response.CodePage = 65001'. – AnthonyWJones