티스토리 뷰

반응형

이전 포스트에서는 App에서 Database를 생성하고, 데이터를 CRUD할 수 있는 환경을 만드는 것이였다. 하지만, 이미 만들어진 Database에 있는 자료를 App과 함께 배포하고, 사용해야 하는 경우도 있다. 그런 경우에는 어떻게 처리를 해야하는지 알아 보기로 하자.

 

이전 포스트의 소스가 필요하다.

Using SQLite in Windows 8 RP Metro style app Part 1

http://kaki104.tistory.com/133

 

1. Sample Database 다운로드

 

Chinook Database

http://chinookdatabase.codeplex.com/

위의 프로젝트에서 모든 종류의 Sample Database file을 다운로드 받을 수 있으며, 다운로드 페이지에서 SQLite용을 다운 받는다.

 

Chinook_Sqlite.sqlite 파일을 복사해서 프로젝트에 붙여넣기 -> Build Action : Content로 변경

 

2. Customer.cs 추가

 

테이블에 데이터를 사용하기 위해서는 데이터 모델 클래스를 추가해 주어야 한다.

 

Chinook Database - Versions 1.1 to 1.3 (Current Release)

http://chinookdatabase.codeplex.com/wikipage?title=Chinook_Schema&referringTitle=Home

 

모델 다이어그램을 참고하면되고, 마스터성 테이블인 Customer 테이블의 데이터를 조회를 하기위해 Customer.cs를 추가한다.

 

    public class Customer
    {
        [PrimaryKey]
        public int CustomerId { get; set; }

        [MaxLength(40)]
        public string FirstName { get; set; }

        [MaxLength(20)]
        public string LastName { get; set; }

        [MaxLength(80)]
        public string Company { get; set; }

        [MaxLength(70)]
        public string Address { get; set; }

        [MaxLength(40)]
        public string City { get; set; }

        [MaxLength(40)]
        public string State { get; set; }

        [MaxLength(40)]
        public string Country { get; set; }

        [MaxLength(10)]
        public string PostalCode { get; set; }

        [MaxLength(24)]
        public string Phone { get; set; }

        [MaxLength(24)]
        public string Fax { get; set; }

        [MaxLength(60)]
        public string Email { get; set; }

        public int SupportRepId { get; set; }

    }

 

3. MainPage.xaml 수정

 

<!--데이터가 많아서 스크롤뷰어를 사용-->

<ScrollViewer Grid.Column="1">
    <StackPanel>
        <StackPanel Orientation="Horizontal">
            <Button Content="Chinook Select" HorizontalAlignment="Left" Width="200" Click="Button_Click_2"/>
        </StackPanel>
        <ListBox x:Name="lbChinook"/>
    </StackPanel>
</ScrollViewer>

 

4. MainPage.xaml.cs 수정

 

        const string chinook = "Chinook_Sqlite.sqlite";


 

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            //SQLite Connection 인스턴스, 읽기 전용, 프로젝트에 포함된 Database 이름
            SQLiteConnection conn = new SQLiteConnection(chinook, SQLiteOpenFlags.ReadOnly);
            //Linq를 이용해서 조건 조회
            var result = from kkk in conn.Table<Customer>()
                         where kkk.Email.Contains("gmail")
                         select kkk;

            //ListBox 초기화
            lbChinook.Items.Clear();
            //Display
            foreach (var item in result)
            {
                lbChinook.Items.Add(string.Format("{0}: {1} / {2} / {3} / {4} / {5} / {6}", item.CustomerId, item.FirstName, item.LastName, item.Company, item.Email, item.Phone, item.Fax));
            }
        }

 

 

 

5. 2회에 걸처서 간단하게

SQLite를 사용하는 방법에 대해서 알아 보았다. 추가로 질문할 내용이 있으면, 리플을 달아주기 바란다.

 

Source Code 

SQLiteSample2.zip

반응형
댓글