92 lines
2.9 KiB
C#
92 lines
2.9 KiB
C#
using System.Data.SQLite;
|
|
using System.Diagnostics;
|
|
using System.Text;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Data;
|
|
using System.Windows.Documents;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using System.Windows.Navigation;
|
|
using System.Windows.Shapes;
|
|
|
|
namespace SQLite_test
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for MainWindow.xaml
|
|
/// </summary>
|
|
public partial class MainWindow : Window
|
|
{
|
|
|
|
|
|
private SQLiteConnection _connection;
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
CreateConnectionToDB();
|
|
}
|
|
|
|
private void CreateConnectionToDB()
|
|
{
|
|
|
|
Classes.User user = new Classes.User();
|
|
|
|
|
|
|
|
|
|
string connectionString = "Data Source=mydatabase.db;Version=3;";
|
|
_connection = new SQLiteConnection(connectionString);
|
|
|
|
try
|
|
{
|
|
_connection.Open();
|
|
Debug.WriteLine("Verbinding met de database is geopend!");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine($"Fout bij het openen van de database: {ex.Message}");
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AddRow(object sender, RoutedEventArgs e)
|
|
{
|
|
// first create a table in database if not exists
|
|
CreateConnectionToDB();
|
|
|
|
string tablename = "Users";
|
|
string createTableQuery = ($"CREATE TABLE IF NOT EXISTS {tablename} (Id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT, Age INT)");
|
|
using (SQLiteCommand command = new SQLiteCommand(createTableQuery, _connection))
|
|
{
|
|
try
|
|
{
|
|
command.ExecuteNonQuery();
|
|
Debug.WriteLine("Tabel is aangemaakt of bestaat al.");
|
|
// Now insert a new row
|
|
string insertQuery = $"INSERT INTO {tablename} (Name, Age) VALUES (@Name, @Age)";
|
|
using (SQLiteCommand insertCommand = new SQLiteCommand(insertQuery, _connection))
|
|
{
|
|
insertCommand.Parameters.AddWithValue("@Name", "Nieuwe Naam");
|
|
insertCommand.Parameters.AddWithValue("@Age", 18);
|
|
insertCommand.ExecuteNonQuery();
|
|
Debug.WriteLine("Nieuwe user is toegevoegd.");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.WriteLine($"Fout bij het aanmaken van de tabel: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void DeleteRow(object sender, RoutedEventArgs e)
|
|
{
|
|
// inputbox
|
|
string input = Microsoft.VisualBasic.Interaction.InputBox("Voer de ID in van de rij die u wilt verwijderen:", "Rij Verwijderen", "", -1, -1);
|
|
Debug.WriteLine($"Input: {input}");
|
|
}
|
|
}
|
|
} |