반응형

안녕하세요, 츄르 사려고 코딩하는 집사! 코집사입니다.

이번 글은 C# WPF 프로그래밍을 하면서 데이터베이스(DB)를 생성하고, 연동하여 저장하는

간단한 방법에 대한 글입니다.

 

1. WPF 앱의 프로젝트를 생성합니다.

2. 메인 윈도우에서 버튼을 클릭했을 때, 서브 윈도우가 나오도록 새 창을 만들어 줍니다.

         - 프로젝트 오른쪽 클릭 - 추가 - 창

3. XAML을 코딩해 줍니다.

        - MainWindow

          1) 버튼을 눌렀을 때 SubWindow 창이 나오게 하기위해 Button 생성

        - SubWindow

          1) DB에 저장하기 위한 Name, E-mail, Phone 을 받기 위해 TextBox와 Save Button 생성

4. Student 클래스 파일 생성 

   - SubWindow에서 입력받기 위해 생성

   - 소스 코드

 

using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ContactApp.Classes
{
    class Contact
    {
        [PrimaryKey, AutoIncrement]  //DB 사용 시 주키 및 자동으로 증가로 설정 using SQLite; 선언
        public int Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }

    }
}

 

5. SQLite 설치

         - 프로젝트 오른쪽 클릭 - Nuget - sqlite-net-pcl 설치

6. MainWindow behind Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;

namespace ContactApp
{
    /// 
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            // 버튼을 눌렀을 때, SubWindow를 Show
            NewContactWindow newContactWindow = new NewContactWindow();
            newContactWindow.Show();
        }
    }
}

 

6. SubWindow Behind Code

using ContactApp.Classes;
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;

namespace ContactApp
{
    /// 
    /// NewContactWindow.xaml에 대한 상호 작용 논리
    /// 
    public partial class NewContactWindow : Window
    {
        public NewContactWindow()
        {
            InitializeComponent();
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {

            //SaveButton을 눌렀을 때, 각 TextBox에 있는 내용을 contact에 저장
            Contact contact = new Contact();
            contact.Name = nameTextBox.Text;
            contact.Email = EmailTextBox.Text;
            contact.Phone = phoneTextBox.Text;
           

             
            string databaseName = "Contact.db";
            string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string databasePath = System.IO.Path.Combine(folderPath, databaseName);

            SQLiteConnection connection = new SQLiteConnection(databasePath); //해당 db경로에 Sqlite생성
            connection.CreateTable();
            connection.Insert(contact);
            connection.Close();
                
            this.Close();
        }
    }
}

 

반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기