Private Function CSV_to_DataTable(ByVal fileName As String, ByVal FullPath As String, Optional ByVal IsChangeColType As Boolean = False) As DataTable
Dim strConn As String = String.Empty
Dim oledbConn As OleDbConnection = Nothing
Dim dt As DataTable = New DataTable("dtSampleData")
Try
strConn = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; Extended Properties=""Text;HDR=YES;IMEX=1;FMT=Delimited""", FullPath)
oledbConn = New OleDbConnection(strConn)
oledbConn.Open()
Dim command As OleDbCommand = New OleDbCommand("SELECT * FROM " & fileName, oledbConn)
Dim oleda2 As OleDbDataAdapter = New OleDbDataAdapter(command)
IsChangeColType = False
If IsChangeColType Then
oleda2.FillSchema(dt, SchemaType.Mapped)
For i As Integer = 0 To dt.Columns.Count - 1
dt.Columns(i).DataType = Type.GetType("System.String")
Next
oleda2.Fill(dt)
Else
oleda2.Fill(dt)
End If
Catch ex As Exception
Throw ex
Finally
oledbConn.Close()
End Try
Return dt
End Function
Tag : .NET, Report Others, Web (ASP.NET), VS 2008 (.NET 3.x)