티스토리 뷰

Windows 8 Store app은 기본적으로 local database를 지원하지 않는다. 그리고, Sqlite는 CPU별로 따로 패키지를 만들어야하는 불편함이 존재한다. 그래서, 찾아보다가 로컬 파일 기반으로 db를 관리하는 것을 Korea Bus Information 앱에서 사용해 보았다.

 

장점 : 

1) 사용이 간단하다

2) CPU별로 앱을 만들 필요가 없다

 

단점 :

1) 성능이 뛰어나지는 않다.

2) db의 저장 공간을 많이 차지 한다.

 

1. 참고

WinRT File Based Database

http://winrtdatabase.codeplex.com/

 

참고에 있는 Download를 눌러서 소스와 셈플을 받은 후 실행해서 만들어지는 dll을 사용해야 한다.

Nuget으로 1차 설치해서 Reference에 추가를 한 후 새로 빌드한 dll 파일을 복사해서 넣어준다.

 

 

 

2. PersonModel.cs

 

namespace GridAppSample
{
    public class PersonModel : GridAppSample.Common.BindableBase
    {
        private Guid personID;

        public Guid PersonID
        {
            get { return personID; }
            set
            {
                personID = value;
                OnPropertyChanged();
            }
        }

        private string firstName;

        public string FirstName
        {
            get { return firstName; }
            set
            {
                firstName = value;
                OnPropertyChanged();
            }
        }

        private string lastName;

        public string LastName
        {
            get { return lastName; }
            set
            {
                lastName = value;
                OnPropertyChanged();
            }
        }

        private int age;

        public int Age
        {
            get { return age; }
            set
            {
                age = value;
                OnPropertyChanged();
            }
        }

    }
}

 

3. SamplePageViewModel.cs

 

//완전 중요!!!
using System.Runtime.InteropServices.WindowsRuntime;

namespace GridAppSample
{
    public class SamplePageViewModel : GridAppSample.Common.BindableBase
    {
        public const string DatabaseName = "SampleDb";

        /// <summary>
        /// 데이터베이스
        /// </summary>
        private Database _database;

        private Table<PersonModel> people;
        /// <summary>
        /// 피플 테이블
        /// </summary>
        public Table<PersonModel> People
        {
            get { return people; }
            set
            {
                people = value;
                OnPropertyChanged();
            }
        }

        private PersonModel selectedPerson;
        /// <summary>
        /// 선택된 사람
        /// </summary>
        public PersonModel SelectedPerson
        {
            get { return selectedPerson; }
            set
            {
                selectedPerson = value;
                OnPropertyChanged();
            }
        }


        public SamplePageViewModel()
        {
            if (Windows.ApplicationModel.DesignMode.DesignModeEnabled == false)
            {
                Initialise();
            }
            else
            {
                People = new Table<PersonModel>();
                People.Add(new PersonModel { FirstName = "Park", LastName = "kaki104", Age = 11 });
                People.Add(new PersonModel { FirstName = "Park", LastName = "2222222", Age = 22 });
                People.Add(new PersonModel { FirstName = "Park", LastName = "kaki103", Age = 33 });
                People.Add(new PersonModel { FirstName = "Park", LastName = "4444444", Age = 44 });
                People.Add(new PersonModel { FirstName = "Park", LastName = "kaki105", Age = 55 });
            }
        }

        public async void Initialise()
        {
            var exists = await Database.DoesDatabaseExistsAsync(DatabaseName, StorageLocation.Local);
            //db존재여부 확인
            if (!exists)
            {
                _database = await Database.CreateDatabaseAsync(DatabaseName, StorageLocation.Local);
                //파일 생성시 이름을 주는 방법도 있는데 사용하면 오류남..
                _database.CreateTable<PersonModel>();
                //변경사항 저장
                await _database.SaveAsync();
            }
            else
            {
                //db열기
                _database = await Database.OpenDatabaseAsync(DatabaseName, true, StorageLocation.Local);
            }
            People = await _database.Table<PersonModel>();

            _database.PropertyChanged += new PropertyChangedEventHandler(_database_PropertyChanged);
        }

        void _database_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
        }

        private DelegateCommand addCommand;
        /// <summary>
        /// 추가
        /// </summary>
        public DelegateCommand AddCommand
        {
            get
            {
                if (addCommand == null)
                {
                    addCommand = new DelegateCommand(
                        async _ =>
                        {
                            var newItem = new PersonModel
                            {
                                PersonID = new Guid(),
                                FirstName = "Input First Name",
                                LastName = "Input Last Name",
                                Age = 0
                            };
                            People.Add(newItem);
                            await _database.SaveAsync();
                        });
                }
                return addCommand;
            }
        }

        private DelegateCommand removeCommand;
        /// <summary>
        /// 삭제
        /// </summary>
        public DelegateCommand RemoveCommand
        {
            get
            {
                if (removeCommand == null)
                {
                    removeCommand = new DelegateCommand(
                        async _ =>
                        {
                            if (SelectedPerson != null)
                            {
                                People.Remove(SelectedPerson);
                                await _database.SaveAsync();
                            }
                        });
                }
                return removeCommand;
            }
        }

        private DelegateCommand updateCommand;
        /// <summary>
        /// 수정
        /// </summary>
        public DelegateCommand UpdateCommand
        {
            get
            {
                if (updateCommand == null)
                {
                    updateCommand = new DelegateCommand(
                        async _ =>
                        {
                            await _database.SaveAsync();
                        });
                }
                return updateCommand;
            }
        }

    }
}

 

4. Source

 

WinRT_File_Based_Database_Sample.zip

'Previous Platforms > Samples' 카테고리의 다른 글

Custom Behavior Sample  (0) 2012.12.25
Text file encoding sample  (0) 2012.11.28
WinRT File Based Database sample  (0) 2012.11.12
Tile + BackgroundTask = LiveTile APP!  (0) 2012.10.30
InputPane using Popup sample  (0) 2012.10.29
GridView item drag&drop move sample  (0) 2012.10.15
댓글
댓글쓰기 폼
Total
641,239
Today
153
Yesterday
311
«   2021/10   »
          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            
10-22 12:49
글 보관함