Store Data from a txt file and save to a database with VB


Well I told you that I'm doing extracting the data from a txt file and storing it in a table for them I'm using the tools of:

  • Sql server
  • Visual Basic Window Form

Inside my table you only have these fields:

  • Id int identity (1,1) not null
  • numbercredit [nvarchar] (50) NOT NULL

inside my code this:

  Dim objReader As New StreamReader(OpenFileDialog.FileName)
        Dim sLine As String = ""
        Dim arrText As New ArrayList()

            sLine = objReader.ReadLine()
            If Not sLine Is Nothing Then

            End If
        Loop Until sLine Is Nothing

        Using Con As New SqlConnection(ConfigurationManager.ConnectionStrings("conexion").ConnectionString)
            Using command As New SqlCommand("RegistrarDatosTxt", Con)

                command.CommandType = CommandType.StoredProcedure
                command.Parameters.AddWithValue("@opt", 1)

                For Each sLine In arrText
                    command.Parameters.Add("@numerocredito", SqlDbType.VarChar, 30).Value = sLine

                    MessageBox.Show("Se Generaron Correctamente los Numero de Creditos", "..::Aviso del Sistema::..")
                Dim frm As New Form2

            End Using
        End Using

I commented that if you save information from a txt file but only if you have a line, but if you have more than two lines like this:

does not register and tells me the error that has more established fields. My question is how could I save several lines of txt on my table?

asked by PieroDev 15.02.2017 в 21:37

1 answer


Create a command for each line read and execute. Just like I did here.

answered by 15.02.2017 / 21:54