C# WPF - sqlite 파일이 없는 경우 생성하기

이슬기·2024년 6월 17일
1

C#

목록 보기
3/5
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}");
            }
        }
    }
}

0개의 댓글