How about, I have a Windows Forms application in which in a TextBox
I simulate the name of a table, in another entry the rows that the table will have and in the remaining 4 the 4 columns that will contain the table.
This file generates a CSV file and a txt file with the SQL syntax to create the query. The problem is that I want to add the number of fields that are, it can be 1, 10, 100, and so on. Adding them in TextBox would be a bit complicated and I really can not find a way to do it.
Next I leave images of the generated files and the source code that is what is currently done. I hope you can help me.
The code I use is the following:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Imports System.IO
Public Partial Class MainForm
Public Sub New()
' The Me.InitializeComponent call is required for Windows Forms designer support.
Me.InitializeComponent()
'
' TODO : Add constructor code after InitializeComponents
'
End Sub
Dim nombres As String() = {"Aaron", "Abel", "Abelardo", "Abraham", "Adalberto", "Adolfo", "Adrian", "Agustin", "Alan", "Alejandro", "Benjamin", "Bernardo", "Baldomero", "Baltasar", "Barack", "Josh"}
Dim apellidos As String() = {"Pineda", "Bernal", "Espinoza", "Spindola", "Brisuela", "Gutierrez", "Escarcega", "Muñiz", "Lopez", "Martinez", "Piña", "Vega", "Ortiz", "Barcenas", "Lopez", "Martinez"}
Dim paises As String() = {"Estados Unidos", "Mexico", "Costa Rica", "Jamaica", "Panama", "Haiti", "Colombia", "Venezuela", "Ecuador", "Peru", "Bolivia", "Chile", "Brasil", "Uruguay", "Paraguay", "Argentina"}
Sub BtnCrearClick(sender As Object, e As EventArgs)
Const separador = ";"
Const folder = "C:\Users\desarrollo\Documents\CSV"
Const titulo = "archivo.csv"
Dim headers As String() = {txtID.Text, txtNombre.Text, txtApp.Text, txtApm.Text}
Dim filePath = Path.Combine(folder, titulo)
Dim sw As New StreamWriter(filePath, False, Encoding.UTF8)
sw.WriteLine(String.Join(separador, headers))
Dim aleatorio As New Random
For i = 1 To Convert.ToInt32(txtFilas.Text)
Dim fields As String() = {i.ToString(), nombres(aleatorio.Next(0, nombres.Length)), apellidos(aleatorio.Next(0, apellidos.Length)), paises(aleatorio.Next(0, paises.Length))}
sw.WriteLine(String.Join(separador, fields))
Next
sw.Close
'Dim row As DataRow
'Dim i As Integer
'For i=1 To Convert.ToInt32(txtFilas.Text)
'row=TablaNombres.NewRow()
'row(txtID.Text)=i
'If cbo2.SelectedItem = "Nombres" Then
'row(txtNombre.Text)=nombres(aleatorio.Next(0,nombres.Length))
'ElseIf cbo2.SelectedItem = "Apellidos"
'row(txtNombre.Text)=apellidos(aleatorio.Next(0,apellidos.Length))
'ElseIf cbo2.SelectedItem = "Paises"
'row(txtNombre.Text)=paises(aleatorio.Next(0,paises.Length))
'End If
'If cbo3.SelectedItem="Nombres" Then
'row(txtApp.Text)=nombres(aleatorio.Next(0,nombres.Length))
'ElseIf cbo3.SelectedItem="Apellidos"
'row(txtApp.Text)=apellidos(aleatorio.Next(0,nombres.Length))
'ElseIf cbo3.SelectedItem="Paises"
'row(txtApp.Text)=paises(aleatorio.Next(0, paises.Length))
'End If
'If cbo4.SelectedItem="Nombres" Then
'row(txtApm.Text)=nombres(aleatorio.Next(0,nombres.Length))
'ElseIf cbo4.SelectedItem="Apellidos"
'row(txtApm.Text)=apellidos(aleatorio.Next(0,nombres.Length))
'ElseIf cbo4.SelectedItem="Paises"
'row(txtApm.Text)=paises(aleatorio.Next(0,nombres.Length))
'End If
'TablaNombres.Rows.Add(row)
'Next
'tabla1.DataSource=TablaNombres
'For i=1 To Convert.ToInt32(txtFilas.Text)
'MsgBox("INSERT INTO " & txtTabla.Text & " (" & txtID.Text & ", " & txtNombre.Text & ", " & txtApp.Text & ", " & txtApm.Text & ") VALUES (" & i & ",'" & nombres(aleatorio.Next(0,nombres.Length)) &"','" & apellidos(aleatorio.Next(0,apellidos.Length)) &"','" & paises(aleatorio.Next(0,paises.Length)) &"')", MsgBoxStyle.OkOnly)
'Next
End Sub
Sub BtnSQLClick(sender As Object, e As EventArgs)
Const separador = ";"
Const folder = "C:\Users\desarrollo\Documents\CSV"
Const titulo = "archivo.txt"
'Dim headers As String()={txtID.Text, txtNombre.Text, txtApp.Text, txtApm.Text}
Dim filePath = Path.Combine(folder, titulo)
Dim sw As New StreamWriter(filePath, False, Encoding.UTF8)
'sw.WriteLine(String.Join(separador, headers))
Dim aleatorio As New Random
For i = 1 To Convert.ToInt32(txtFilas.Text)
Dim insert As String = "INSERT INTO " & txtTabla.Text & " (" & txtID.Text & ", " & txtNombre.Text & ", " & txtApp.Text & ", " & txtApm.Text & ") VALUES (" & i & ",'" & nombres(aleatorio.Next(0, nombres.Length)) & "','" & apellidos(aleatorio.Next(0, apellidos.Length)) & "','" & paises(aleatorio.Next(0, paises.Length)) & "')"
sw.WriteLine(String.Join(separador, insert))
Next
sw.Close
End Sub
Sub MainFormLoad(sender As Object, e As EventArgs)
End Sub
End Class
Thanks in advance.