PDA

View Full Version : Help with looping



sue
02-01-2007, 09:28 AM
I am trying to create an asp page that will import data from an excel spreadsheet into MS SQL. The code below works perfectly for importing the first column into the SQL database - could someone please tell me how to import ALL of the columns in the excel spreadsheet? I think I need to change the loop but I'm not sure what to change it to. Thanks for any advice!!!

<%
Set rsXL = server.CreateObject("ADODB.RecordSet")
rsXL.CursorLocation = 3
rsXL.Open "[sheet1$]","Provider=MSDASQL;Driver={Microsoft Excel Driver (*.xls)};DBQ=" & Server.MapPath("Test.xls")' connection String For excell
Dim Conn
Dim oConn
Dim StrSql
'varibles for sqlserver connection strin
' g
Conn = "Provider=SQLOLEDB;Data Source=---;UID=------;PWD=----------;Database=--------;"
Set oConn = Server.CreateObject("Adodb.Connection")
For Each F In rsXL.Fields
Response.Write ("<B>" & F.Name & "</B>" )
' this f column A in your excel file
Next
Response.Write "<BR>"
Do Until rsXL.EOF
For Each F In rsXL.Fields
Response.Write rsXL(F.Name)
StrSql="Insert Into test(vcRecord) values('"& rsXL(F.Name)&"')"
oConn.Open(Conn)
oConn.execute StrSql
oConn.Close()
Next
Response.Write "<BR>"
rsXL.MoveNext
Loop
Set rsXL = Nothing
Set oConn = Nothing
%>

Thank you very much!!!