using System;
using System.Data.SQLite;
using System.IO;
using System.Windows;
namespace YourAppNamespace
{
public partial class MainWindow : Window
{
private string dbFilePath = "your_database_file.sqlite";
public MainWindow()
{
InitializeComponent();
try
{
if (!File.Exists(dbFilePath))
{
SQLiteConnection.CreateFile(dbFilePath);
CreateDatabase();
}
using (var connection = new SQLiteConnection($"Data Source={dbFilePath};Version=3;"))
{
connection.Open();
}
}
catch (Exception ex)
{
MessageBox.Show($"데이터베이스 초기화 중 오류가 발생했습니다: {ex.Message}");
Application.Current.Shutdown();
}
}
private void CreateDatabase()
{
try
{
using (var connection = new SQLiteConnection($"Data Source={dbFilePath};Version=3;"))
{
connection.Open();
string sql = "CREATE TABLE IF NOT EXISTS YourTableName (Id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT)";
using (var command = new SQLiteCommand(sql, connection))
{
command.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
MessageBox.Show($"데이터베이스 생성 중 오류가 발생했습니다: {ex.Message}");
Application.Current.Shutdown();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
using System.Diagnostics;
namespace LG_RFID.Utils
{
internal class SqliteDb
{
private string strConn;
private string filePath;
private string connectionString;
public SqliteDb()
{
try
{
filePath = System.Environment.CurrentDirectory;
strConn = System.IO.Path.Combine(filePath, "sqlite.db");
connectionString = $"Data Source={strConn};Version=3;";
if (!System.IO.File.Exists(strConn))
{
SQLiteConnection.CreateFile(strConn);
}
using (SQLiteConnection conn = new SQLiteConnection(connectionString))
{
conn.Open();
SQLiteCommand cmd = new SQLiteCommand(sql, conn);
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
Debug.WriteLine($"데이터베이스 초기화 중 오류가 발생했습니다: {ex}");
}
}
}
}