How to use ASP & Upload CSV and import to database This is learn/tutorial asp developers how to using aspĀ Upload CSV and import to database
ShotDev Focus:
- ASP & Upload CSV and import to database
Example
asp_csv_import1.asp
<%Option Explicit%> <html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <form action="asp_csv_import2.asp" method="post" enctype="multipart/form-data" name="frmMain"> Upload <input name="file1" type="file"> <input type="submit" name="Submit" value="Submit"> </form> </body> </html>
asp_csv_import2.asp
<%Option Explicit%> <html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <% Dim objFSO,oInStream,sRows,arrRows Dim Conn,strSQL,objExec Dim sFileName Dim mySmartUpload '*** Upload By aspSmartUpload ***' '*** Create Object ***' Set mySmartUpload = Server.CreateObject("aspSmartUpload.SmartUpload") '*** Upload Files ***' mySmartUpload.Upload '** Getfile Name ***' sFileName = mySmartUpload.Files("file1").FileName If sFileName <> "" Then '*** Upload **' mySmartUpload.Files("file1").SaveAs(Server.MapPath("shotdev/" & sFileName)) '*** Create Object ***' Set objFSO = CreateObject("Scripting.FileSystemObject") '*** Check Exist Files ***' If Not objFSO.FileExists(Server.MapPath("shotdev/" & sFileName)) Then Response.write("File not found.") Else '*** Open Files ***' Set oInStream = objFSO.OpenTextFile(Server.MapPath("shotdev/" & sFileName),1,False) '*** open Connect to Access Database ***' Set Conn = Server.Createobject("ADODB.Connection") Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("shotdev/mydatabase.mdb"),"" , "" Do Until oInStream.AtEndOfStream sRows = oInStream.readLine arrRows = Split(sRows,",") '*** Insert to table customer2 ***' strSQL = "" strSQL = strSQL &"INSERT INTO customer2 " strSQL = strSQL &"(CustomerID,Name,Email,CountryCode,Budget,Used) " strSQL = strSQL &"VALUES " strSQL = strSQL &"('"&arrRows(0)&"','"&arrRows(1)&"','"&arrRows(2)&"' " strSQL = strSQL &",'"&arrRows(3)&"','"&arrRows(4)&"','"&arrRows(5)&"') " Set objExec = Conn.Execute(strSQL) Set objExec = Nothing Loop oInStream.Close() Conn.Close() Set oInStream = Nothing Set Conn = Nothing End If Response.write ("CSV import to access completed.") End IF %> </body> </html>
Create a asp file and save to path root-path/myasp/
Run
http://localhost/myasp/asp_csv_import1.asp
Screenshot
2wrinkle…
…