Database creation VB.Net Exercise - Visual Basic Programming Course


 Exercise

Database creation

Objetive

Create a program to ask the user for data about books (title, author, genre, and summary) and store them in a SQLite database.

Code

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 - Systems Tutorials and Programming Courses © 2024 All rights reserved.  Legal Conditions.