Practice Exercises C# Sharp - Learn to program with performing exercises C# Sharp

Practice Exercises C# Sharp

Learn to program with performing exercises C# Sharp


SQL to text - Practice Exercises C# Sharp


Lesson 8:

File management


Exercise 8.39:

SQL to text


Objetive:

You must create a C # program capable of parsing SQL language INSERT commands and extracting their data to separate lines of text, as follows: if the input file contained these three lines:

insert into people (name, address, age) values ('smith, pedro', 'your street', 23);

insert into people (name, address, age) values ('juan', 'calle cinco, 6', 24);

insert into cities (code, name) values ('a', 'alicante');

The resulting file should have on each line the name of a field, followed by a "colon" and its value. In addition, each record must be preceded by the name of the table and followed by a blank line, like this:

persons name: smith, pedro address: your street age: 23

persons name: juan Address: 5th Street, 6 age: 24

cities code: a name: 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.