Question: Create SQL statements from a flat txt (C #)

2

Good afternoon, I am trying to create a small program to convert plain text (.txt) into another document with SQL statements (insert), usually I usually deal with other functionalities or projects and it has caught me with a little low guard, Does anyone have a documentation of how to serialize a flat data in txt to create dynamic INSERTS from the information given?

example:

1manual 004
2automatic 001 34533 2345
1manual 005
1manual 007
2automatic 002 45644 3456

the idea would be to distinguish between manual and automatic and create a different insert for each one.

PS: I do not want a solution (if it's simple I would not mind), but above all I look for documentation and ways to execute this small program:)

thanks.

    
asked by Bigby 04.10.2016 в 16:07
source

3 answers

3

Well, since it's something simple, I'll give you an example code. In this code an example input file is read with the data that you have given us, and the sentences are written in another file. It would be something like this:

string[] filas=File.ReadAllLines("c:\temp\sentencias.txt");
StreamWriter sw = new StreamWriter("c:\temp\sentencias.sql");
foreach (string fila in filas)
{
     string sql = "INSERT ";
     string[] campos = fila.Split(' ');

     if (campos[0]== "1manual")
     {
          //Insert de tipo manual
          sql+= " INTO TABLA1 (CAMPO1) VALUES (" + campos[1] +");";
     }
     else
     {
          //insert de tipo automatico
          sql += " INTO TABLA2 (CAMPO1,CAMPO2,CAMPO3) VALUES (" + campos[1] + "," + campos[2] + "," + campos[3] + ");";
     }
     sw.WriteLine(sql);
}

sw.Close();

This generates in the output a file similar to this:

INSERT  INTO TABLA1 (CAMPO1) VALUES (004);
INSERT  INTO TABLA2 (CAMPO1,CAMPO2,CAMPO3) VALUES (001,34533,2345);
INSERT  INTO TABLA1 (CAMPO1) VALUES (005);
INSERT  INTO TABLA1 (CAMPO1) VALUES (007);
INSERT  INTO TABLA2 (CAMPO1,CAMPO2,CAMPO3) VALUES (002,45644,3456);
    
answered by 04.10.2016 / 16:47
source
1

Finally, with the help of Pikoh, I managed to find the solution:

string mydate = DateTime.Now.ToString("yyyyMMdd");
                    string AÑO = DateTime.Now.ToString("yyyy");
                    string MES = DateTime.Now.ToString("MM");
                    string DIA = DateTime.Now.ToString("dd");
                    string sql = "INSERT ";
                    string[] campos = fila.Split(' ');

                    if (campos[0].StartsWith("1H"))
                    {

                        sql += "INTO TABLE (VALUES,VALUES,VALUES) VALUES (" + "'" + mydate + "'" + "," + "'" + campos[0].Substring(1, 8) + "'" + "," + "'" + campos[0].Substring(9, 7) + "'" + "," + "'" + campos[8] + "'" + ");";

inserting data, dates and manipulating text strings has taught me a lot, but now I have another problem:

If I want to apply a "backspace" to a specific string, how could I do it? (I mean a backspace to a specific string).

Greetings

    
answered by 06.10.2016 в 14:22
0

An important point that I notice is how to process the data in the file, I would advise you to evaluate using

filehelpers

with this you could map by a fixed width or by some separator each value of the lines and convert this to a list of a class that you define

This allows you to have each data of the file to generate the queries, it will only be a question of traversing each entity in the paired list and generating the INSERT in a string

    
answered by 04.10.2016 в 17:31