Export DATATABLE in VB.NET

0

Hello that good night, I have the following code that creates a table with 4 columns and the number of rows is provided by the user, this table is exported to a DataGridView but if I generate more than one million records it takes a little bit. Then I would like to know if the DataTable can be exported directly to CSV. The code I use is the following:

Sub BtnCrearClick(sender As Object, e As EventArgs)
    Dim IDColumna As New DataColumn(txtID.Text)
    IDColumna.DataType=GetType(Integer)
    IDColumna.AutoIncrement=True

    Dim Nombre As New DataColumn(txtNombre.Text)
    Nombre.DataType=GetType(String)

    Dim App As New DataColumn(txtApp.Text)
    App.DataType=GetType(String)

    Dim Apm As New DataColumn(txtApm.Text)
    Apm.DataType=GetType(String)

    Dim TablaNombres As New DataTable(txtTabla.Text)
    TablaNombres.Columns.Add(IDColumna)
    TablaNombres.Columns.Add(Nombre)
    TablaNombres.Columns.Add(App)
    TablaNombres.Columns.Add(Apm)

    TablaNombres.Constraints.Add("Key1", IDColumna, True)

    Dim nombres As String()={"Aaron","Abel","Abelardo","Abraham","Adalberto","Adolfo","Adrian","Agustin","Alan","Alejandro",
        "Benjamin","Bernardo","Baldomero","Baltasar","Barack"}
    Dim apellidos As String()={"Pineda", "Bernal", "Espinoza", "Spindola", "Brisuela", "Gutierrez", "Escarcega", "Muñiz", "Lopez", "Martinez", "Piña",
        "Vega", "Ortiz", "Barcenas", "Lopez", "Martinez"}
    Dim aleatorio As New Random

    Dim row As DataRow
    Dim i As Integer
    For i=0 To Convert.ToInt32(txtFilas.Text)
        row=TablaNombres.NewRow()
        row(txtID.Text)=i
        row(txtNombre.Text)=nombres(aleatorio.Next(0,nombres.Length))
        row(txtApp.Text)=apellidos(aleatorio.Next(0,nombres.Length))
        row(txtApm.Text)=apellidos(aleatorio.Next(0,nombres.Length))
        TablaNombres.Rows.Add(row)
    Next

    tabla1.DataSource=TablaNombres
End Sub

'

    
asked by Guillermo Ricardo Spindola Bri 05.06.2016 в 04:15
source

1 answer

1

Yes, of course you can directly create a csv file as you generate the records.

It would come to something like this:

Private Sub BtnCrear_Click(sender As Object, e As EventArgs) Handles BtnCrear.Click
    Const fieldSeparator = ";"
    Const folderPath = "C:\CSVDocuments"
    Dim headers As String() = { txtID.Text, txtNombre.Text, txtApp.Text, txtApm.Text }
    Dim nombres As String()={"Aaron","Abel","Abelardo","Abraham","Adalberto","Adolfo","Adrian","Agustin","Alan","Alejandro",
        "Benjamin","Bernardo","Baldomero","Baltasar","Barack"}
    Dim apellidos As String()={"Pineda", "Bernal", "Espinoza", "Spindola", "Brisuela", "Gutierrez", "Escarcega", "Muñiz", "Lopez", "Martinez", "Piña",
        "Vega", "Ortiz", "Barcenas", "Lopez", "Martinez"}
    Dim filePath = Path.Combine(folderPath, $"{txtTabla.Text}.csv")
    Dim sw As New StreamWriter(filePath, False, Encoding.UTF8)
    sw.WriteLine(String.Join(fieldSeparator, headers))
    Dim aleatorio As New Random()
    For i=0 To Convert.ToInt32(txtFilas.Text)
        Dim fields As String() = {
            i.ToString(),
            nombres(aleatorio.Next(0, nombres.Length - 1)),
            apellidos(aleatorio.Next(0, apellidos.Length -1)),
            apellidos(aleatorio.Next(0, apellidos.Length - 1))
        }
        sw.WriteLine(String.Join(fieldSeparator, fields))
    Next
    sw.Close()
End Sub

The constant fieldSeparator contains the separator to be used between the fields.

The folderPath constant contains the path to the folder in which the file should be saved.

The Array headers contains the names of the fields and is included as the first line of the file. If you do not want to include the names of the fields you can remove that part.

For the rest, I'm simply generating the values just like you did and I'm adding them to the file using a StreamWriter.

To read and write data in text files you can also use a free library such as FileHelpers . Although for such a simple case I do not think it's worth it.

    
answered by 05.06.2016 в 10:30