web 2.0

ASP.NET(vb.net) & Upload and Import CSV to Database

ASP.NET(vb.net) & Upload and Import CSV to Database - In this article we will example scripts ASP.NET how to upload CSV to server and import CSV to database.

ShotDev Focus:
- ASP.NET(vb.net) & Upload and Import CSV to Database

Example

AspNetUploadCsvToDatabase.aspx

  1. <%@ Import Namespace="System.Data"%>  
  2. <%@ Import Namespace="System.Data.OleDb"%>  
  3. <%@ Page Language="VB" %>  
  4. <script runat="server">  
  5. Sub btnUpload_OnClick(sender As Object, e As EventArgs)  
  6. Dim strPath As String = "csv/"  
  7. Dim dt As DataTable  
  8. If Me.fiUpload.HasFile Then  
  9.   
  10. Me.fiUpload.SaveAs(Server.MapPath(strPath & fiUpload.FileName))  
  11.   
  12. '*** Read CSV to DataTable ***'  
  13. dt = CsvCreateDataTable(strPath,fiUpload.FileName)  
  14.   
  15. '*** Insert to Database ***'  
  16. InsertToDatabase(dt)  
  17. End IF  
  18. End Sub  
  19.   
  20. '*** Convert CSV to DataTable ***'  
  21. Function CsvCreateDataTable(ByVal strPath,ByVal strFilesName) As DataTable  
  22. Dim objConn As New OleDbConnection  
  23. Dim dtAdapter As OleDbDataAdapter  
  24. Dim dt As New DataTable  
  25.   
  26. Dim strConnString As String  
  27. strConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="&Server.MapPath(strPath) & _  
  28. ";Extended Properties='TEXT;HDR=Yes;FMT=Delimited;Format=Delimited(,)'"  
  29.   
  30. objConn = New OleDbConnection(strConnString)  
  31. objConn.Open()  
  32.   
  33. Dim strSQL As String  
  34. strSQL = "SELECT * FROM " & strFilesName  
  35.   
  36. dtAdapter = New OleDbDataAdapter(strSQL, objConn)  
  37. dtAdapter.Fill(dt)  
  38.   
  39. dtAdapter = Nothing  
  40.   
  41. objConn.Close()  
  42. objConn = Nothing  
  43.   
  44. Return dt '*** Return DataTable ***'  
  45.   
  46. End Function  
  47.   
  48. Function InsertToDatabase(ByVal dt)  
  49.   
  50. Dim objConn As System.Data.OleDb.OleDbConnection  
  51. Dim objCmd As System.Data.OleDb.OleDbCommand  
  52. Dim strConnString,strSQL As String  
  53. Dim i As Integer  
  54.   
  55. strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";"  
  56. objConn = New System.Data.OleDb.OleDbConnection(strConnString)  
  57. objConn.Open()  
  58.   
  59. '*** Loop Insert ***'  
  60. For i = 0 To dt.Rows.Count - 1  
  61.   
  62. Try  
  63. strSQL = "INSERT INTO customer2 (CustomerID,Name,Email,CountryCode,Budget,Used) " & _  
  64. "VALUES ('"&dt.Rows(i)("CustomerID")&"','"&dt.Rows(i)("Name")&"','"&dt.Rows(i)("Email")&"'" & _  
  65. " ,'"&dt.Rows(i)("CountryCode")&"','"&dt.Rows(i)("Budget")&"','"&dt.Rows(i)("Used")&"')"  
  66. objCmd = New System.Data.OleDb.OleDbCommand()  
  67. With objCmd  
  68. .Connection = objConn  
  69. .CommandType = CommandType.Text  
  70. .CommandText = strSQL  
  71. End With  
  72. objCmd.ExecuteNonQuery()  
  73. Me.lblText.Text = Me.lblText.Text & "["&dt.Rows(i)("CustomerID")&"] Inserted <br>"  
  74. Catch err As Exception  
  75. Me.lblText.Text = Me.lblText.Text & "["&dt.Rows(i)("CustomerID")&"] Not Insert <br>"  
  76. End Try  
  77.   
  78. Next  
  79.   
  80. objCmd = Nothing  
  81. objConn.Close()  
  82. objConn = Nothing  
  83.   
  84. End Function  
  85.   
  86. </script>  
  87. <html>  
  88. <head>  
  89. <title>ShotDev.Com Tutorial</title>  
  90. </head>  
  91. <body>  
  92. <form id="form1" runat="server">  
  93. <asp:FileUpload id="fiUpload" runat="server"></asp:FileUpload>  
  94. <input id="btnUpload" type="button" OnServerClick="btnUpload_OnClick"  value="Upload" runat="server" />  
  95. <hr />  
  96. <asp:Label id="lblText" runat="server"></asp:Label>  
  97. </form>  
  98. </body>  
  99. </html>  

Screenshot

ASP.NET(vb.net) & Upload and Import CSV to Database

ASP.NET(vb.net) & Upload and Import CSV to Database
.
.
.
Download this script.
Download

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

Leave a Reply

You must be logged in to post a comment.