2012-03-29 3 views
1

나는 Windows phone 용 간단한 응용 프로그램을 만들고 있습니다. 데이터베이스를 만들고 싶고 데이터베이스에 여러 항목 (10 개까지 허용)을 갖고 싶습니다. 나는 초보자이고 내가 본 모든 튜토리얼은 sth가 "add"또는 sth와 같은 버튼에 데이터베이스에 항목을 추가하는 것에 관한 것입니다. 필자는 데이터베이스에있는 여러 항목을 갖고 싶어하므로 사용자가 사용할 수 있도록 준비해야합니다. 이것을 어떻게 할 수 있습니까? 내가 아직 초보자이기 때문에 분명한 방법으로 저에게 편지를 보내주십시오. 예제 또는 튜토리얼 링크를 제공 할 수 있다면 좋을 것입니다. 고마워!windows phone 항목으로 데이터베이스 생성

답변

2

사전로드 된 DB가 필요한 경우 응용 프로그램에 sqlCe DB를 추가하고 시드 데이터로 db를 채울 수 있습니다. 그러면 DBContext 생성자가 호출되는 동안 DB 파일을 ISO 저장소에 복사 할 수 있습니다.

public Moviadb1DataContext (string connectionString) : base(connectionString) 
    { 
     IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication(); 

     if (!iso.FileExists("Moviadb1.sdf")) 
     { 
      MoveReferenceDatabase(); 
     } 

     if (!DatabaseExists()) 
      CreateDatabase(); 
    } 

    public static void MoveReferenceDatabase() 
    { 
     // Obtain the virtual store for the application. 
     IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication(); 

     // Create a stream for the file in the installation folder. 
     using (Stream input = Application.GetResourceStream(new Uri("Moviadb1.sdf", UriKind.Relative)).Stream) 
     { 
      // Create a stream for the new file in isolated storage. 

      using (IsolatedStorageFileStream output = iso.CreateFile("Moviadb1.sdf")) 
      { 
       // Initialize the buffer. 
       byte[] readBuffer = new byte[4096]; 
       int bytesRead = -1; 

       // Copy the file from the installation folder to isolated storage. 
       while ((bytesRead = input.Read(readBuffer, 0, readBuffer.Length)) > 0) 
       { 
        output.Write(readBuffer, 0, bytesRead); 
       } 
      } 
     } 
    } 

데이터의 양이 매우 적은 경우 참조 DB를 이동하는 대신 시드 데이터를 추가 할 수도 있습니다.

public ListenDataDataContext (string connectionString) : base(connectionString) 
    { 
     if (!DatabaseExists()) 
     { 
      CreateDatabase(); 
      List<Audiables> PreLoads = new List<Audiables>(); 
      PreLoads.Add(new Audiables { Category = 1, Name = "I want To Eat", AudioLocation = "Sounds/Food/1_IwantToEat.wma", ImageLocation = "Images/Food/1_IwantToEat.jpg" }); 
      PreLoads.Add(new Audiables { Category = 1, Name = "I want To Drink", AudioLocation = "Sounds/Food/1_IwantToDrink.wma", ImageLocation = "Images/Food/1_IwantToDrink.jpg" }); 

      PreLoads.Add(new Audiables { Category = 2, Name = "I want A Ticket", AudioLocation = "Sounds/Travel/1_IwantATicket.wma", ImageLocation = "Images/Travel/1_IwantATicket.jpg" }); 
      PreLoads.Add(new Audiables { Category = 2, Name = "I want To Sit", AudioLocation = "Sounds/Travel/1_IwantToSit.wma", ImageLocation = "Images/Travel/1_IwantToSit.jpg" }); 

      PreLoads.Add(new Audiables { Category = 3, Name = "How Much Is That", AudioLocation = "Sounds/Shopping/1_HowMuchIsThat.wma", ImageLocation = "Images/Shopping/1_HowMuchIsThat.jpg" }); 
      PreLoads.Add(new Audiables { Category = 3, Name = "Please Take the Money", AudioLocation = "Sounds/Shopping/1_PleaseTakeTheMoney.wma", ImageLocation = "Images/Shopping/1_PleaseTakeTheMoney.jpg" }); 
      Audiables.InsertAllOnSubmit(PreLoads); 
      this.SubmitChanges(); 
     } 
    } 

만드는 해피 응용 프로그램 :

+0

내가 사용 App.xaml.cs를 이 뭔가를 얻었다 (FoodDataContext의 dB = 새로운 FoodDataContext (FoodDataContext.DBConnectionString)) { \t \t \t \t 경우 (db.DatabaseExists() == false) { // 데이터베이스 만들기 db.CreateDatabase(); \t \t \t \t } } 과 Main.xaml.cs에 내가 내 DB를로드 물건을 가지고 있지만 매번 내가 최대 onNagiatedTo 방법은 데이터베이스의 항목을 만드는 시동 거기 onNavigatedTo 방법이이 항목을 중복 이것을 막을 수있는 방법이 있습니까? – Sandra

+0

미리로드 할 데이터가 적은 경우 app.xaml.cs에서 두 번째 방법을 사용하고 다른 곳에서는 첫 번째 방법을 동일한 위치에서 시도하십시오 –