Results 1 to 8 of 8

Thread: Import date from CSV to sql table with sqlbulkcopy

Threaded View

  1. #8
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: Import date from CSV to sql table with sqlbulkcopy

    Hi GideonE,

    try this just to get it to a Datagridview first..
    set a Filter if tha Data is to large

    Code:
    Imports System.Data.OleDb
    
    
    'Dim SQL As String = "Select * From [Adressen.csv] Where AD_Land Like 'D%';"
    'Dim SQL As String = "Select * From [Adressen.csv] Where AD_Name1 Like  '" & SearchBox & "%';"
    'Dim SearchBox As String
    'SearchBox = TextBox1.Text
    
    
    Public Class Form5
    
     
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim _tb As New DataTable
    
            Me.DataGridView1.DataSource = _tb
    
            Dim SrcDir As String = "C:\"
            Dim sConn As String = String.Join(";", New String() { _
               "Provider=Microsoft.Jet.OLEDB.4.0", _
               "Data Source=" & SrcDir, _
               "Extended Properties=""Text; HDR=Yes; FMT=Delimited"""})
    
          
            Dim SQL As String = "Select * From [Adressen.csv];"
    
            Using Cn As New OleDbConnection(sConn), ta As New OleDbDataAdapter(SQL, Cn)
                Cn.Open()
                ta.Fill(_tb)
            End Using
        End Sub
    
    'Copy to new Table, I know this is Access you have to change to SQL DB
     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim sSql As String
            Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=D:\NWind.mdb")
            'copy CSV to new Table in Access
            'the CSV/Location is = C:\Adressen.csv
            sSql = "Select * Into CSVImport From [Adressen.csv] IN 'c:\' 'Text;'"
            con.Open()
            ExecuteSQL(con, sSql)
            con.Close()
            con = Nothing
        End Sub
        Public Function ExecuteSQL(ByVal Con As OleDb.OleDbConnection, _
                                        ByVal sSQL As String, _
                                        Optional ByRef ErrMessage As String = Nothing, _
                                        Optional ByVal TransAction As  _
                                        OleDb.OleDbTransaction = Nothing) As Integer
            ErrMessage = Nothing
            Try
                Dim Result As Integer = 0
                Using Cmd As New OleDb.OleDbCommand(sSQL, Con, TransAction)
                    Result = Cmd.ExecuteNonQuery
                End Using
                Return Result
            Catch ex As Exception
                ErrMessage = ex.Message
                Return 0
            End Try
        End Function
    
    
    End Class
    regards
    Chris
    Last edited by ChrisE; Jan 27th, 2018 at 02:38 AM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width