10.02 - Database query

Objetive

Create a program to display the data about books which your previous prorgram has stored.

Solution

using System;
using System.Data.SQLite;

namespace ReadSQL
{
    class Program
    {
        static void Main(string[] args)
        {

            SQLiteConnection conexion = new SQLiteConnection 
            ("Data Source=ejemplo01.sqlite;Version=3;New=False;Compress=True;");
            
            conexion.Open();
            
            // Lanzamos la consulta y preparamos la estructura para leer datos 
            
            string consulta = "select * from books";
            SQLiteCommand cmd = new SQLiteCommand(consulta, conexion);
            SQLiteDataReader datos = cmd.ExecuteReader();
            // Leemos los datos de forma repetitiva 

            while (datos.Read())
            {
                string name = Convert.ToString(datos[0]);
                string autor = Convert.ToString(datos[1]);
                string genre = Convert.ToString(datos[2]);
                string summary = Convert.ToString(datos[3]);

                // Y los mostramos 
                System.Console.WriteLine("Name: {0}, Autor: {1}, Genre: {2}, Summary: {3}",
                        name, autor, genre, summary);
            }
            // Finalmente, cerramos la conexion 
            conexion.Close();
        }
    }
}


Popular Posts