Creación de bases de datos Ejercicio VB.Net - Curso de Programación Visual Basic

 Ejercicio

Creación de bases de datos

Objetivo

Cree un programa para solicitar al usuario datos sobre los libros (título, autor, género y resumen) y guárdelos en una base de datos SQLite.

Código

Imports System
Imports System.Data.SQLite
Imports System.IO
Public Class DatabaseCreation
    Private Shared Sub Main(ByVal args As String())
        Dim conexion As SQLiteConnection
        Dim cmd As SQLiteCommand

        Try
            conexion = New SQLiteConnection("Data Source=ejemplo01.sqlite;Version=3;" & " New=True; Compress=True;")
            conexion.Open()

            If Not File.Exists("ejemplo01.sqlite") Then
                Dim creacion As String = "create table books (" & " title varchar(50), autor varchar(50)," & " genre varchar(50), summary varchar(50))"
                cmd = New SQLiteCommand(creacion, conexion)
                cmd.ExecuteNonQuery()
            End If

            Dim line As String
            Dim title As String = "", autor As String = "", genre As String = "", summary As String = ""

            Do
                Console.Write("Title: ")
                line = Console.ReadLine()
                If line IsNot Nothing Then title = line
                Console.Write("Autor: ")
                line = Console.ReadLine()
                If line IsNot Nothing Then autor = line
                Console.Write("Genre: ")
                line = Console.ReadLine()
                If line IsNot Nothing Then genre = line
                Console.Write("Summary: ")
                line = Console.ReadLine()
                If line IsNot Nothing Then summary = line
                Dim insercion As String = "insert into books values ('" & title & "', '" & autor & "','" & genre & "', '" & summary & "')"
                cmd = New SQLiteCommand(insercion, conexion)
                cmd.ExecuteNonQuery()
                Console.WriteLine("Insert OK!")
            Loop While line IsNot Nothing

            conexion.Close()
        Catch e As Exception
            Console.WriteLine("Error" & e.Message)
        End Try
    End Sub
End Class

Juan A. Ripoll - Tutoriales de Sistemas y Cursos de Programación© 2024 Todos los derechos reservados.  Condiciones legales.