G
Guest
I am using a datareader to cycle through a list of records one at a time. I
frequently get connection timeouts and am wondering what I am doing wrong.
In more detail, I retrieve an excel spreadsheet provided by the user into a
datatable (dtExcel), then go through each record to see if a matching record
exists in our sql database. Basically, if there is a match, I mark the
excel data "Matched", if not, mark it "unmatched". GetDataReader is a
separate data layer function. I assume the problem is the GetDataReader
doesn't close the connection, but if I close the connection in the
datalayer, it doesn't return the data reader. So I close the data reader in
the presentation layer, but am still getting time outs when there is a large
excel spreadsheet. Do I have to use a dataset, because with a dataset I can
close the connection??
Psuedo-code:
Public Sub CheckForMatchingRecords
for i=0 to dt.Rows.Count-1
qry = "Select id from tbl where exclid = '" &
dt.rows(i).Item(3).tostring & "'"
Dim dr as sqldatareader
dr = GetDataReader(connString, qry)
if not drOPTransactions.Read
dt.Rows(i).BeginEdit
dt.Rows(i).Item(icol-3) = "NO MATCH"
dt.Rows(i).endedit
else
dt.Rows(i).BeginEdit
dt.Rows(i).Item(icol-3) = "MATCH"
dt.Rows(i).endedit
end if
dr.close
end sub
Public Function GetDataReader(ByVal connString as String, ByVal qry as
String) as SQLDataReader
Dim cn as SqlConnection = New SqlConnection(connString)
Dim dr as SQLDataReader
Dim cmd As New SqlCommand(qry, cn)
Try
cn.Open()
dr = cmd.ExecuteReader
Return dr
Catch ex As SqlClient.SqlException
....
End Try
End Function
frequently get connection timeouts and am wondering what I am doing wrong.
In more detail, I retrieve an excel spreadsheet provided by the user into a
datatable (dtExcel), then go through each record to see if a matching record
exists in our sql database. Basically, if there is a match, I mark the
excel data "Matched", if not, mark it "unmatched". GetDataReader is a
separate data layer function. I assume the problem is the GetDataReader
doesn't close the connection, but if I close the connection in the
datalayer, it doesn't return the data reader. So I close the data reader in
the presentation layer, but am still getting time outs when there is a large
excel spreadsheet. Do I have to use a dataset, because with a dataset I can
close the connection??
Psuedo-code:
Public Sub CheckForMatchingRecords
for i=0 to dt.Rows.Count-1
qry = "Select id from tbl where exclid = '" &
dt.rows(i).Item(3).tostring & "'"
Dim dr as sqldatareader
dr = GetDataReader(connString, qry)
if not drOPTransactions.Read
dt.Rows(i).BeginEdit
dt.Rows(i).Item(icol-3) = "NO MATCH"
dt.Rows(i).endedit
else
dt.Rows(i).BeginEdit
dt.Rows(i).Item(icol-3) = "MATCH"
dt.Rows(i).endedit
end if
dr.close
end sub
Public Function GetDataReader(ByVal connString as String, ByVal qry as
String) as SQLDataReader
Dim cn as SqlConnection = New SqlConnection(connString)
Dim dr as SQLDataReader
Dim cmd As New SqlCommand(qry, cn)
Try
cn.Open()
dr = cmd.ExecuteReader
Return dr
Catch ex As SqlClient.SqlException
....
End Try
End Function