i have a textbox to upload .csv file. after i click upload it insert the .csv file into sql server database
how to insert .csv file using vb.net into sql server

Recommended Answers

All 4 Replies

u can use function Split to separate each parts into fields and insert them into database
hope it will help

u can use function Split to separate each parts into fields and insert them into database
hope it will help

do u have any example of it.because i just learn vb.net today.i need some example to learn

While Not EOF(7)
Input #7, InputRead
panjang_rec = Len(InputRead)
arrayRek = Split(InputRead, ";", 8)
temp.path_file = arrayRek(0)
temp.nama_file = arrayRek(1)
temp.nama_penyanyi = arrayRek(2)
temp.judul_lagu = arrayRek(3)
temp.nama_album = arrayRek(4)
temp.genre = arrayRek(6)
temp.durasi = arrayRek(7)
'Debug.Print nama_file
recpos = LOF(15) / reclengtemp + 1
temp.nomor_lagu = recpos
Put #15, recpos, temp
Wend
Close #7
sorry but it was VB6 version, but i it will give u the idea
temp is the structure which contains the record
arrayrek just a buffer variable
hope it works

i have this code

Private Sub Upload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Upload.Click

        Dim Colsname As String
        Dim Colscontacts As String
        Dim oFile As System.IO.File
        Dim oRead As System.IO.StreamReader

        Dim ErrClient As Boolean

        Try
            oRead = oFile.OpenText("C:\sms_contacts.csv")
        Catch ex As Exception
            ErrClient = True
            MessageBox.Show("Error while retrieving records on table0..." & ex.Message, "Load Records0")

        Finally
        End Try
        Try
            Do While oRead.Peek <> -1
                Dim tmp As String = oRead.ReadLine()
                If Not tmp = "" Then
                    Dim Cols() As String = Split(tmp, ",")
                    If Cols(0) <> "" Then
                        Colsname = Cols(0)
                        Colscontacts = Cols(1)
                    End If
                End If
            Loop
            Dim objSQLConn As New SqlConnection(objDTCS.GetSQLHeadCountConnectionString)
            Dim objSQLComm As New SqlCommand
            Dim SQLStr As String
            Dim objSQLAdap As New SqlDataAdapter
            objSQLConn.Open()
            objSQLComm = objSQLConn.CreateCommand
            objSQLAdap.SelectCommand = objSQLComm

            SQLStr = " INSERT INTO sms_contact(name, contacts) Values (" _
              & "'" & ("colsname") & "', " _
              & "'" & ("colscontacts") & "') "



            objSQLComm.CommandText = SQLStr
            objSQLComm.ExecuteNonQuery()


            objSQLConn.Close()

        Catch ex As Exception

            MessageBox.Show("Error while retrieving records on table1..." & ex.Message, "Load Records1")
            Exit Sub
        Finally

        End Try

    End Sub

im stuck at the insert.i dont know should i put into the values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.