web 2.0

How to use ASP & Upload CSV and import to database

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

  1. <%Option Explicit%>  
  2. <html>  
  3. <head>  
  4. <title>ShotDev.Com Tutorial</title>  
  5. </head>  
  6. <body>  
  7. <form action="asp_csv_import2.asp" method="post" enctype="multipart/form-data" name="frmMain">  
  8. Upload  
  9. <input name="file1" type="file">  
  10. <input type="submit" name="Submit" value="Submit">  
  11. </form>  
  12. </body>  
  13. </html>  

asp_csv_import2.asp

  1. <%Option Explicit%>  
  2. <html>  
  3. <head>  
  4. <title>ShotDev.Com Tutorial</title>  
  5. </head>  
  6. <body>  
  7. <%  
  8. Dim objFSO,oInStream,sRows,arrRows  
  9. Dim Conn,strSQL,objExec  
  10. Dim sFileName  
  11. Dim mySmartUpload  
  12.   
  13. '*** Upload By aspSmartUpload ***'  
  14.   
  15. '*** Create Object ***'  
  16. Set mySmartUpload = Server.CreateObject("aspSmartUpload.SmartUpload")  
  17.   
  18. '*** Upload Files ***'  
  19. mySmartUpload.Upload  
  20.   
  21. '** Getfile Name ***'  
  22. sFileName = mySmartUpload.Files("file1").FileName  
  23.   
  24. If sFileName <> "" Then  
  25. '*** Upload **'  
  26. mySmartUpload.Files("file1").SaveAs(Server.MapPath("shotdev/" & sFileName))  
  27.   
  28. '*** Create Object ***'  
  29. Set objFSO = CreateObject("Scripting.FileSystemObject")  
  30.   
  31. '*** Check Exist Files ***'  
  32. If Not objFSO.FileExists(Server.MapPath("shotdev/" & sFileName)) Then  
  33. Response.write("File not found.")  
  34. Else  
  35.   
  36. '*** Open Files ***'  
  37. Set oInStream = objFSO.OpenTextFile(Server.MapPath("shotdev/" & sFileName),1,False)  
  38.   
  39. '*** open Connect to Access Database ***'  
  40. Set Conn = Server.Createobject("ADODB.Connection")  
  41. Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("shotdev/mydatabase.mdb"),"" , ""  
  42.   
  43. Do Until oInStream.AtEndOfStream  
  44. sRows = oInStream.readLine  
  45. arrRows = Split(sRows,",")  
  46. '*** Insert to table customer2 ***'  
  47. strSQL = ""  
  48. strSQL = strSQL &"INSERT INTO customer2 "  
  49. strSQL = strSQL &"(CustomerID,Name,Email,CountryCode,Budget,Used) "  
  50. strSQL = strSQL &"VALUES "  
  51. strSQL = strSQL &"('"&arrRows(0)&"','"&arrRows(1)&"','"&arrRows(2)&"' "  
  52. strSQL = strSQL &",'"&arrRows(3)&"','"&arrRows(4)&"','"&arrRows(5)&"') "  
  53. Set objExec = Conn.Execute(strSQL)  
  54. Set objExec = Nothing  
  55. Loop  
  56.   
  57. oInStream.Close()  
  58. Conn.Close()  
  59. Set oInStream = Nothing  
  60. Set Conn = Nothing  
  61.   
  62. End If  
  63.   
  64. Response.write ("CSV import to access completed.")  
  65.   
  66. End IF  
  67. %>  
  68. </body>  
  69. </html>  

Create a asp file and save to path root-path/myasp/

Run
http://localhost/myasp/asp_csv_import1.asp

Screenshot

ASP & Upload CSV and import to database

ASP & Upload CSV and import to database

.
.
.
Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (4 votes, average: 7.75 out of 10)
Loading ... Loading ...

One Response to “How to use ASP & Upload CSV and import to database”

  1. 2wrinkle…

Leave a Reply

You must be logged in to post a comment.