Large variable memory problems - memory

I'm looping through a table in my MSSQL database, and building up a variable to hold some data. There's 3000+ rows in the table.
My problem is that the system loads forever when I call the function. If I disable the variable-buildup, the query executes in a few seconds.
Example:
Set rs = dbConn.Execute("Select x, y, z From MyTable")
Do until rs.EOF
strData = strData & rs("x") & ","
strData = strData & rs("y") & ","
strData = strData & rs("z") & vbCrlf
rs.MoveNext
Loop
Set rs = Nothing
Finally I do:
Response.Clear
Response.Buffer = True
Response.AddHeader "Content-Disposition", "attachment;filename=export.csv"
Response.ContentType = "application/octet-stream"
Response.Write strData
Response.End
How can I do this instead?

Related

ADO adLockBatchOptimistic allows updates on modified records

I'm begging for help, since I'm too stupid.
What I do:
Open connection to Access DB
Download Table to a Recordset
Disconnect the RecordSet
Update a record
Update the same record in DB (by another user)
Connect RecordSet back to DB
UpdateBatch affected record
Yesterday it was throwing an error that the record has been modified in the meantime.
Today it's updating the record without any issues.
I would bet my arm I haven't changed anything...
I open the recordset with following settings (first set them, then open):
.CursorLocation = adUseClient
.LockType = adLockBatchOptimistic
I beg everyone for help
Example Code:
Dim DB_FOLDER_PATH As String
Dim DB_FILE_NAME As String
Dim DB_FILE_PATH As String
Dim CONNECTION As ADODB.CONNECTION
Dim CONNECTION_STRING As String
Dim QUERY_STRING As String
Dim tmp_RS As ADODB.Recordset
Dim tmp_RS2 As ADODB.Recordset
DB_FOLDER_PATH = "\\XXXXX\userdata\XXXXX\home\Documents\Data Base\"
DB_FILE_NAME = "TEST"
DB_FILE_PATH = DB_FOLDER_PATH & DB_FILE_NAME & ".accdb"
Set CONNECTION = New ADODB.CONNECTION
CONNECTION_STRING = "Provider=Microsoft.ACE.OLEDB.12.0" & ";" & "Data Source=" & DB_FILE_PATH & ";" & "Persist Security Info=False"
CONNECTION.Open CONNECTION_STRING
QUERY_STRING = "SELECT" & " " & "*" & " FROM [" & "DATA" & "]" & ";"
Set tmp_RS = New ADODB.Recordset
With tmp_RS
.CursorLocation = adUseClient
.LockType = adLockBatchOptimistic
.Open QUERY_STRING, CONNECTION
.ActiveConnection = Nothing
End With
Set tmp_RS2 = New ADODB.Recordset
With tmp_RS2
.CursorLocation = adUseClient
.LockType = adLockBatchOptimistic
.Open QUERY_STRING, CONNECTION
.ActiveConnection = Nothing
End With
With tmp_RS
.Fields("FIELD_LONG_TEXT_PLAIN").Value = "ABC"
.Update
.ActiveConnection = CONNECTION
.UpdateBatch
End With
With tmp_RS2
.Fields("FIELD_LONG_TEXT_PLAIN").Value = "ZXC"
.Update
.ActiveConnection = CONNECTION
.UpdateBatch
End With
Best Regards,
Michal

How to read quoted field from CSV using VBScript

In a sample.csv file, which has fixed number of columns, I have to extract a particular field value and store it in a variable using VBScript.
sample.csv
100,SN,100.SN,"100|SN| 435623| serkasg| 15.32|
100|SN| 435624| serkasg| 15.353|
100|SN| 437825| serkasg| 15.353|"," 0 2345"
101,SN,100.SN,"100|SN| 435623| serkasg| 15.32|
100|SN| 435624| serkasg| 15.353|
100|SN| 437825| serkasg| 15.353|"," 0 2346"
I want to parse the last two fields which are within double quotes and store them in two different array variables for each row.
You could try using an ADO connection
Option Explicit
dim ado: set ado = CreateObject("ADODB.Connection")
ado.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\txtFilesFolder\;Extended Properties=""text;HDR=No;FMT=Delimited"";"
ado.open
dim recordSet: set recordSet = ado.Execute("SELECT * FROM [samples.csv]")
dim field3, field4
do until recordSet.EOF
field3 = recordSet.Fields(3).Value
field4 = recordSet.Fields(4).Value
' use your fields here
recordSet.MoveNext
loop
recordSet.close
ado.close
You may have an issue if those fields are greater than 255 characters in length - if they are, they may return truncated. You also may have better luck with ODBC or ACE connection strings instead of the Jet one I've used here.
Since CSV's are comma-separated, you can use the Split() function to separate the fields into an array:
' Read a line from the CSV...
strLine = myCSV.ReadLine()
' Split by comma into an array...
a = Split(strLine, ",")
Since you have a static number of columns (5), the last field will always be a(4) and the second-to-last field will be a(3).
Your CSV data seems to contain 2 embedded hard returns (CR, LF) per line. Then the first line ReadLine returns is:
100,SN,100.SN,"100|SN| 435623| serkasg| 15.32|
The solution below unwraps these lines before extracting the required fields.
Option Explicit
Const ForReading = 1
Const ForAppending = 8
Const TristateUseDefault = 2 ' Opens the file using the system default.
Const TristateTrue = 1 ' Opens the file as Unicode.
Const TristateFalse = 0 ' Opens the file as ASCII.
Dim FSO, TextStream, Line, LineNo, Fields, Field4, Field5
ExtractFields "sample.csv"
Sub ExtractFields(FileName)
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(FileName) Then
Line = ""
LineNo = 0
Set TextStream = FSO.OpenTextFile(FileName, ForReading, False, TristateFalse)
Do While Not TextStream.AtEndOfStream
Line = Line & TextStream.ReadLine()
LineNo = LineNo + 1
If LineNo mod 3 = 0 Then
Fields = Split(Line, ",")
Field4 = Fields(3)
Field5 = Fields(4)
MsgBox "Line " & LineNo / 3 & ": " & vbNewLine & vbNewLine _
& "Field4: " & Field4 & vbNewLine & vbNewLine _
& "Field5: " & Field5
Line = ""
End If
Loop
TextStream.Close()
Else
MsgBox "File " & FileName & " ... Not found"
End If
End Sub
Here is an alternative solution that allows for single or multiline CSV records. It uses a regular expression which simplifies the logic for handling multiline records. This solution does not remove CRLF characters embedded in a record; I've left that as an exercise for you :)
Option Explicit
Const ForReading = 1
Const ForAppending = 8
Const TristateUseDefault = 2 ' Opens the file using the system default.
Const TristateTrue = 1 ' Opens the file as Unicode.
Const TristateFalse = 0 ' Opens the file as ASCII.
Dim FSO, TextStream, Text, MyRegExp, MyMatches, MyMatch, Field4, Field5
ExtractFields "sample.csv"
Sub ExtractFields(FileName)
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(FileName) Then
Set MyRegExp = New RegExp
MyRegExp.Multiline = True
MyRegExp.Global = True
MyRegExp.Pattern = """([^""]+)"",""([^""]+)"""
Set TextStream = FSO.OpenTextFile(FileName, ForReading, False, TristateFalse)
Text = TextStream.ReadAll
Set MyMatches = MyRegExp.Execute(Text)
For Each MyMatch in MyMatches
Field4 = SubMatches(0)
Field5 = SubMatches(1)
MsgBox "Field4: " & vbNewLine & Field4 & vbNewLine & vbNewLine _
& "Field5: " & vbNewLine & Field5, 0, "Found Match"
Next
Set MyMatches = Nothing
TextStream.Close()
Else
MsgBox "File " & FileName & " ... Not found"
End If
End Sub

Can I use httprequest to post a file automatically, in the same way I might with a regular upload?

I'm stuck on something that (I think) should be quite simple, but I can't work out how to do it. I can't find any information either here on SO or on Google.
We have a web app that takes data from our site and automatically POSTS this to another API.
We'd like to send over a binary file also - for example, we'd grab the file from a local URL and POST it's contents over to the other site.
Could anybody point me in the right direction for this? For example, would I need to embed the file URL in the data somehow and POST as multipart/form-data?
I created these Classic ASP function to POST a file to another server. Hopefully it will help you too.
Function PostDocument(intDocumentID, binFile, strFilename, strContentType)
Dim objHttp, strBoundary, strRequestStart, strRequestEnd, binPost
Dim objStream
strBoundary = "---------------------------9849436581144108930470211272"
Set objHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
strRequestStart = "--" & strBoundary & vbCrlf &_
"Content-Disposition: form-data; name=""id""" & vbCrlf &_
vbCrlf &_
intDocumentID & vbCrlf &_
vbCrlf &_
"--" & strBoundary & vbCrlf &_
"Content-Disposition: form-data; name=""file""; filename=""" & strFilename & """" & vbCrlf &_
"Content-Type: " & strContentType & vbCrlf &_
vbCrlf
strRequestEnd = vbCrLf & "--" & strBoundary & "--"
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = adTypeBinary '1
objStream.Mode = adModeReadWrite '3
objStream.Open
objStream.Write StringToBinary(strRequestStart)
objStream.Write binFile
objStream.Write StringToBinary(strRequestEnd)
objStream.Position = 0
binPost = objStream.Read
Response.Write binPost
objStream.Close
Set objStream = Nothing
objHttp.Open "POST", "(url removed)", False, "(username removed)", "(password removed)"
objHttp.setRequestHeader "Content-Type", "multipart/form-data; boundary=""" & strBoundary & """"
objHttp.Send binPost
PostDocument = objHttp.ResponseText
Set objHttp = Nothing
End Function
Function StringToBinary(toConvert)
Dim objStream, data
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Charset = "ISO-8859-1"
objStream.Type = adTypeText '2
objStream.Mode = adModeReadWrite '3
objStream.Open
objStream.WriteText toConvert
objStream.Position = 0
objStream.Type = adTypeBinary '1
StringToBinary = objStream.Read
objStream.Close
Set objStream = Nothing
End Function

Parsing POST request with unexpected URL encoding

This question follows an earlier one.
Here is some code that reproduces the problem:
POST:
str = "accountRequest=<NewUser>" & vbLf & _
"Hello" & vbTab & "World" & vbLf & _
"</NewUser>"
Set objHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
objHTTP.open "POST", "service.asp", False
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.send str
response.Write(objHTTP.responseText)
Set objHTTP = Nothing
service.asp:
function w (str)
response.Write(str & "<br>")
end function
str = request.Form("accountRequest")
w(str)
w("Tabs: "& InStr(str,vbTab))
w("Lines: "& InStr(str,vbLf))
output:
HelloWorld
Tabs: 0
Lines: 0
Can anyone please help?
Try:
Replace(Request.Form("accountRequest"), vbLF, vbCRLF))
Or:
Replace(Request.Form("accountRequest"), vbLF, "<br>"))|
Depending on where you're displaying it, either should work.
Or possibly this:
Function URLDecode(sConvert)
Dim aSplit
Dim sOutput
Dim I
If IsNull(sConvert) Then
URLDecode = ""
Exit Function
End If
' convert all pluses to spaces
sOutput = REPLACE(sConvert, "+", " ")
' next convert %hexdigits to the character
aSplit = Split(sOutput, "%")
If IsArray(aSplit) Then
sOutput = aSplit(0)
For I = 0 to UBound(aSplit) - 1
sOutput = sOutput & _
Chr("&H" & Left(aSplit(i + 1), 2)) &_
Right(aSplit(i + 1), Len(aSplit(i + 1)) - 2)
Next
End If
URLDecode = sOutput
End Function
From here: http://www.aspnut.com/reference/encoding.asp
If they are coming across the wire as the actual "\" and "n" characters, you can do a replace on those characters with the appropriate vbCRLF and vbTAB constants.
Finally figured out that ASP Request.Form method doesn't preserve tabs if they're in the "\t" format (as opposed to URL encoded). However, PHP's $_POST does.

ADODB.Recordset error '800a0e78' - Error when I pass 2 parameters (ASP/MSSQL)

My classic ASP page thorws up an error when I pass 2 paremeters to the Exec code for my
stored procedure. Basically I want to send 1 paremeter to cover the column I am looking for
and one for the search-term. For example imSchool, New York University. I have data checks on the data sent as well as Record Set.State code showing that everytime i choose SQL Query with the two paramerts the error "Operation is not allowed when the object is closed." always shows.
I tried to open the object at " While Not rs.EOF" line of code where it error out bit no luck.
I am thinking my SQLQuery is bad because when I run the Stored Procedure in MSSQL with the same input I get a return table everytime. SQL Server 2008 R2 and Classic ASP.
HERE IS MY IF STATMENT WITH SQLQUERY CODE (keep in mind the 1st one works fine and data is selected from the DB)
SQLQuery = "Exec sp_vw_InternImport"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.CursorType = 3
rs.Open SQLQuery, OBJdbConnection
If filterColmn <> "" Then
SQlQuery = "Exec sp_vw_InternImport_ColID #LookUpID=N'" + filterID + "'" + ", #LookUpCol=N'" + filterID + "'"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.CursorType = 3
rs.Open SQLQuery, OBJdbConnection
End If
HERE IS MY STORED PROCEDURE CODE
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_vw_InternImport_ColID]
(
#LookUpID nvarchar (255),
#LookUpCol nvarchar (50)
)
AS
SET NOCOUNT ON
BEGIN
IF #LookUpCol = 'imYear'
SELECT * FROM v_InternImport WHERE imYear = #LookUpID
ELSE
IF #LookUpCol = 'imSchool'
SELECT * FROM v_InternImport WHERE imSchool = #LookUpID
ELSE
IF #LookUpCol = 'imDiscipline'
SELECT * FROM v_InternImport WHERE imDiscipline = #LookUpID
IF #LookUpCol = 'imDegree'
SELECT * FROM v_InternImport WHERE imDegree = #LookUpID
END
When passing arguments to stored procedure directly, you don't have to "assign" the parameters. Doing this probably results in the whole value passed (e.g. #LookUpCol will have the value of #LookUpCol ='imYear') thus your SP won't select anything and you have empty and closed recordset.
Try having such code instead:
SQlQuery = "Exec sp_vw_InternImport_ColID '" & filterID & "', '" & filterID & "'"
You can't use the same object twice at the same time (like rs), you have to make it another name or you have to close it and start over.. This should work:
SQLQuery = "Exec sp_vw_InternImport"
If filterColmn <> "" Then SQLQuery = "Exec sp_vw_InternImport_ColID #LookUpID=N'" + filterID + "'" + ", #LookUpCol=N'" + filterID + "'"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.CursorType = 3
rs.Open SQLQuery, OBJdbConnection
rs.Close
Or in your example, if you close the first rs object before you go on to the next, it may help.
set rs = nothing ... then .... set rs = server.createobject
So like this:
SQLQuery = "Exec sp_vw_InternImport"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.CursorType = 3
rs.Open SQLQuery, OBJdbConnection
rs.Close ------ Close it before you re-open it
If filterColmn <> "" Then
SQlQuery = "Exec sp_vw_InternImport_ColID #LookUpID=N'" + filterID + "'" + ", #LookUpCol=N'" + filterID + "'"
rs.Open SQLQuery, OBJdbConnection
rs.Close ------ Always Close your Objects!
End If

Resources