We help IT Professionals succeed at work.
Get Started

Is there a better way to insert blob images from SQL to Access DB?

Cashmgmt
Cashmgmt asked
on
335 Views
Last Modified: 2012-08-13
I'm currently copying blob images from sql to access.  It works fine the way I'm doing it, but I've noticed that the query uses a lot of resources on the server.  Is there a better way I can do this?  I've pasted the code.  Any help would be greatly appreciated.  Thanks!
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "SELECT * FROM "&lbxblobtable&" WHERE d_busdate >= CAST('"&BeginDate&"' AS smalldatetime) AND d_busdate <= CAST('"&EndDate&"' AS smalldatetime)", Connection

Set objFirstTable = Server.CreateObject("ADOX.Table")
objFirstTable.Name = ""&lbxblobtable&""

for each column in rs.fields 
ct = column.type 
	if ct = 200 then 
		ct = 202
	elseif ct = 131 then 
		ct = 3		
	else
	end if 
	objFirstTable.Columns.Append column.name, ct
next
	
objADOXDatabase.Tables.Append objFirstTable 
		
Set Connection2 = Server.CreateObject("ADODB.Connection")

Set sqlcmd = CreateObject("ADODB.Command")

Connection2.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("WebLITS\App\"&randomNumber&"_"&lockboxname&".mdb") 

Connection2.Open

If rs.EOF Then 
'	Response.Write("No BLOB Records to Update!") 
'	Response.write "<br>"  
'	Response.write "<br>" 
Else

'	for each column in rs.fields
'	st = st & column.name & ", "
'	next
'	st = left(st,len(st)-2)

Do While NOT rs.Eof
	SQL = " "
	for each column in rs.fields
	SQL = SQL & " ?,"
	next
	SQL = left(SQL,len(SQL)-1)

	sqlcmd.ActiveConnection = Connection2
	sqlcmd.CommandType = 1
	SqlStmt = "INSERT INTO "&lbxblobtable&" VALUES (" & SQL & ")"
	sqlcmd.CommandText = SqlStmt

   	sqlcmd.Parameters.Append sqlcmd.CreateParameter("ID", 3, 1, , rs("ID"))
	sqlcmd.Parameters.Append sqlcmd.CreateParameter("BUSDATE", 202, 1, Len(Trim(rs("D_BUSDATE"))), Trim(rs("D_BUSDATE")))
	sqlcmd.Parameters.Append sqlcmd.CreateParameter("REFNO", 202, 1, Len(Trim(rs("D_REFNO"))), Trim(rs("D_REFNO")))
	sqlcmd.Parameters.Append sqlcmd.CreateParameter("IMAGE", 205, 1, 2147483647, rs("imgData"))
	Set sqlrst = sqlcmd.Execute()

	sqlcmd.Parameters.Delete ("ID")
	sqlcmd.Parameters.Delete ("BUSDATE")
	sqlcmd.Parameters.Delete ("REFNO")	
	sqlcmd.Parameters.Delete ("IMAGE")		
	rs.MoveNext     	
Loop
End If

rs.Close

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE