0% found this document useful (0 votes)
22 views

Using System

The document contains C# code that defines methods to create a SQLite database, insert data into a database table, delete data from the table, and read data from the table. It defines a Car class with ID, make, model, and license plate properties. It prompts the user to add or remove car data, executes SQL commands to perform the related CRUD operations, and displays the contents of the database table.

Uploaded by

hrymobilni
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Using System

The document contains C# code that defines methods to create a SQLite database, insert data into a database table, delete data from the table, and read data from the table. It defines a Car class with ID, make, model, and license plate properties. It prompts the user to add or remove car data, executes SQL commands to perform the related CRUD operations, and displays the contents of the database table.

Uploaded by

hrymobilni
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System.Data.

SqlClient;
using System.Data.SQLite;

namespace MySQL
{
internal class Program
{
const string NameOfDB = "MyDB.db";
const string ConStr = $"Data Source={NameOfDB};Version=3;";
static void CreateDB()
{
if (!File.Exists(NameOfDB))
{
SQLiteConnection.CreateFile(NameOfDB);

string command = "CREATE TABLE IF NOT EXISTS Cars(" +


"ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"Znacka nvarchar(15), " +
"Model nvarchar(15)," +
"SPZ nvarchar(7));";

using (SQLiteConnection conn = new SQLiteConnection(ConStr))


{
conn.Open();
SQLiteCommand cmd = new SQLiteCommand(command, conn);
cmd.ExecuteNonQuery();
conn.Close();
}
}
}

static void VlozData()


{
Console.WriteLine("Chcete přidat data? [a/n]");
string pokracovat = Console.ReadLine();
while (pokracovat == "a")
{
Console.WriteLine("Zadej značku auta: ");
string Znacka = Console.ReadLine();
Console.WriteLine("Zadej model auta: ");
string Model = Console.ReadLine();
Console.WriteLine("Zadej SPZ auta: ");
string SPZ = Console.ReadLine();

using (SQLiteConnection conn = new SQLiteConnection(ConStr))


{
conn.Open();
string cmdtext = "INSERT INTO Cars(Znacka,Model,SPZ) Values" +
"(@Znacka,@Model,@SPZ);";
SQLiteCommand cmd = new SQLiteCommand(cmdtext, conn);

cmd.Parameters.AddWithValue("@Znacka", Znacka);
cmd.Parameters.AddWithValue("@Model", Model);
cmd.Parameters.AddWithValue("@SPZ", SPZ);

cmd.ExecuteNonQuery();
conn.Close();
}
Console.WriteLine("Chcete pokračovat v zadávání? [a/n]");
pokracovat = Console.ReadLine();

}
Console.WriteLine("Data byla úspěšně vložena!");
}

static void PrectiData()


{
Console.WriteLine("\nObsah databáze:\n\n".ToUpper());
using (SQLiteConnection conn = new SQLiteConnection(ConStr))
{
conn.Open();
SQLiteCommand cmd = new SQLiteCommand();
cmd.CommandText = "SELECT * FROM Cars";
cmd.Connection = conn;
SQLiteDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("Značka: {0}\t Model: {1}\t SPZ:{2}",
reader["Znacka"], reader["Model"], reader["SPZ"]);
}
Console.WriteLine("\nVýpis dokončen!\n");
}
}

static void OdstranData()


{
string model = "";
try
{
Console.WriteLine("Chcete odebrat data? [a/n]");
string dotaz = Console.ReadLine();
while(dotaz == "a")
{
Console.WriteLine("Zadej model vozu, který chcete odstranit:");
model = Console.ReadLine();

using (SQLiteConnection conn = new SQLiteConnection(ConStr))


{
string cmdtxt = "DELETE FROM Cars WHERE Model = @model";
conn.Open();
SQLiteCommand cmd = new SQLiteCommand(cmdtxt, conn);
cmd.Parameters.AddWithValue("@model", model);
cmd.ExecuteNonQuery();
conn.Close();
}
Console.WriteLine("Chcete pokračovat v odstraňování? [a/n]");
dotaz = Console.ReadLine();
}

}
catch (SqlException sqlex)
{
Console.WriteLine("Model ({0}) buď neexistuje nebo nebyl správně zadán!", model);
}
}

static void Main(string[] args)


{
CreateDB();
VlozData();
OdstranData();
PrectiData();
}
}
}

You might also like