본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

WPF & C# - SQLite Sample ( 샘플 / SQLlite)

728x90
반응형
 WPF & C# - SQLite Sample ( 샘플 / SQLlite)

 

 

관련자료

WPF & C# - SQLite 설치하기 ( 에러발생 시 조치 방안 )

WPF & C# - SQLite 에러 조치 방안

 

test_SqlLite.zip
1.19MB

SQLlite Sample

 

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
string PassWord()
{
    string Password = "Password = password2";
    return Password;
}
 
string sqliteFile = Environment.CurrentDirectory + "\\" + @"mySQLite5.db";   // SQLlite 저장 파일명
        
 
private void CreateDB_Click(object sender, RoutedEventArgs e)
{
    // 파일이 없을 시 생성
    SQLiteConnection.CreateFile(sqliteFile);
    string connString = @"Data Source = " + sqliteFile + ";";
 
    // DB 파일에 테이블을 생성하는 코드
    using (SQLiteConnection conn = new SQLiteConnection(connString))
    {
        conn.SetPassword("password2");
        conn.Open();
        // 테이블 및 필드생성
        string sql = "create table members (name varchar(20), age int)";
 
        SQLiteCommand command = new SQLiteCommand(sql, conn);
        int result = command.ExecuteNonQuery();
 
        // 인덱스 설정
        sql = "create index idx01 on members(name)";
        command = new SQLiteCommand(sql, conn);
        result = command.ExecuteNonQuery();
        command.Dispose();
        conn.Close();
    }
 
    // 생성한 테이블에 Row를 추가하는 코드
    using (SQLiteConnection conn = new SQLiteConnection(connString + PassWord()))
    {
        conn.Open();
 
        // 필드에 데이터 추가
        String sql = "insert into members (name, age) values ('홍길동', 32)";
        SQLiteCommand command = new SQLiteCommand(sql, conn);
        int result = command.ExecuteNonQuery();
 
        // 필드에 데이터 추가
        String sql2 = "insert into members (name, age) values ('레오나르도다빈치', 30)";
        SQLiteCommand command2 = new SQLiteCommand(sql2, conn);
        int result2 = command2.ExecuteNonQuery();
 
        command.Dispose();
        command2.Dispose();
        conn.Close();
    }
 
 
    // 테이블에 저장된 데이터에 대한 조회 코드
    using (SQLiteConnection conn = new SQLiteConnection(connString + PassWord()))
    {
        conn.Open();
        String sql = "select * from members";
 
        SQLiteCommand cmd = new SQLiteCommand(sql, conn);
        SQLiteDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            MessageBox.Show(rdr["name"+ " " + rdr["age"]);
        }
        rdr.Close();
        cmd.Dispose();
        conn.Close();
    }
}
 
private void btn2_Click(object sender, RoutedEventArgs e)
{
    string connString = @"Data Source = " + sqliteFile + ";";
 
    // 테이블에 저장된 데이터에 대한 조회 코드
    using (SQLiteConnection conn = new SQLiteConnection(connString + PassWord()))
    {
        conn.Open();
        String sql = "select * from members";
 
        SQLiteCommand cmd = new SQLiteCommand(sql, conn);
        SQLiteDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            MessageBox.Show(rdr["name"+ " " + rdr["age"]);
        }
        rdr.Close();
        cmd.Dispose();
        conn.Close();
    }
}
}
}
 
cs
 
 

 

 

 

SQLite 이미지 첨부하여 추가 시 예제

 

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// sqlite ADD 예제
using (SQLiteConnection conn = new SQLiteConnection(strConn))
{
    conn.Open();
 
    // 이미지 파일을 바이너리 형태로 형변환
    byte[] imageBytes = null;
    using (MemoryStream ms = new MemoryStream())
    {
        System.Windows.Media.Imaging.BmpBitmapEncoder bbe = new BmpBitmapEncoder();
        bbe.Frames.Add(BitmapFrame.Create(new Uri(img.Source.ToString(), UriKind.RelativeOrAbsolute)));
        bbe.Save(ms);
        imageBytes = ms.ToArray();
    }
 
    // 필드에 데이터 추가
    String sql = "insert into data (smtp, img) values ('홍길동', @img2)";
 
    SQLiteCommand command = new SQLiteCommand(sql, conn);
    SQLiteParameter param = new SQLiteParameter("@img2"System.Data.DbType.Binary);
    param.Value = imageBytes;
    command.Parameters.Add(param);
    int result = command.ExecuteNonQuery();
}
 
cs
 

 

 

 

 

SQLite Delete 예제

 

 
1
2
3
4
5
6
7
8
9
10
// SQLite 삭제문
using (SQLiteConnection conn = new SQLiteConnection(connString))
{
    string sql = "DELETE FROM sign WHERE date ='" + str + "'";
    using (SQLiteCommand cmd = new SQLiteCommand(sql, conn))
    {
        conn.Open();
        int result = cmd.ExecuteNonQuery();
    }
}
cs
 

 

 

 

 

관련자료

WPF & C# - SQLite 설치하기 ( 에러발생 시 조치 방안 )

WPF & C# - SQLite 에러 조치 방안

 

 

 

728x90
반응형