Tutorial C# Sharp - Learn to program with performing exercises C# Sharp

Tutorial C# Sharp

Learn to program with performing exercises C# Sharp

SQL to text - Tutorial C# Sharp - Lesson 8, Exercise 39 -
Tutorial C# Sharp4,85581245

SQL to text - Tutorial C# Sharp


Lesson 8:

File management


Exercise 8.39:

SQL to text


Objetive:

Debes crear un programa en C# capaz de analizar órdenes INSERT de lenguaje SQL y de extraer sus datos a líneas de texto independientes, de la siguiente forma: si el fichero de entrada contuviera estas tres líneas:

insert into personas (nombre, direccion, edad) values ('smith, pedro', 'su calle', 23);
insert into personas (nombre, direccion, edad) values ('juan', 'calle cinco, 6', 24);
insert into ciudades (codigo, nombre) values ('a', 'alicante');

el fichero resultante debería tener en cada línea el nombre de un campo, seguido de “dos puntos” y de su valor. Además, cada registro deberá estar precedido del nombre de la tabla y seguido por una línea en blanco, así:

personas
nombre: smith, pedro
direccion: su calle
edad: 23

personas
nombre: juan
direccion: calle cinco, 6
edad: 24

ciudades
codigo: a
nombre: alicante


Source Code:


using System;
using System.IO;
namespace SQL2text
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader ficheroEntrada = null;
            string linea;
            string nombre;

            Console.WriteLine("Not enough parameters!");
            Console.Write("Enter file name: ");
            nombre = Console.ReadLine();

            try
            {
                ficheroEntrada = File.OpenText(nombre);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            try
            {
                do
                {
                    linea = ficheroEntrada.ReadLine();
                    if (linea != null)
                    {
                        Console.WriteLine();
                        string tableName = linea.Substring(12).Split(' ')[0];
                        string[] campo = linea.Substring(linea.IndexOf("(") + 1,
                                linea.IndexOf(")") - linea.IndexOf("(") - 1).Split(',');
                        string[] valores = linea.Substring(linea.IndexOf("values (") + 9,
                                linea.IndexOf(");") - linea.IndexOf("values (") - 9).Split(',');

                        Console.WriteLine(tableName);
                        for (int i = 0; i < campo.Length; i++)
                        {
                            Console.Write(campo[i].Trim() + ": ");
                            Console.WriteLine(valores[i].Trim().Replace("'", ""));
                        }
                    }
                } while (linea != null);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.ReadLine();
        }
    }
}
Exercisey 8.39






Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.