2012-03-29 77 views
1

我正在爲windows phone構建一個簡單的應用程序。我想創建一個數據庫,並希望在數據庫中有幾個(可以說10個)項目。我是一個初學者,我看過的每一個教程都是關於在數據庫中添加項目按鈕「添加」或這樣的東西。我不需要這樣做,因爲我想讓數據庫中有多個項目,以供用戶使用。我怎樣才能做到這一點?請以清楚的方式給我寫信,因爲我仍然是初學者。如果您可以提供一些示例或教程的鏈接,那就太好了。謝謝你!windows phone用物品創建數據庫

回答

2

如果您需要預加載的數據庫,那麼您可以在您的應用程序中添加一個sqlCe數據庫,並使用您的種子數據填充數據庫。 然後您可以在調用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); 
       } 
      } 
     } 
    } 

如果您的數據量非常少,您還可以添加一些種子數據,而不是移動參考數據庫。

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我有方法的OnNavigatedTo那裏我有一個加載了我的分貝的東西,但每次我啓動了起來onNagiatedTo方法使得在數據庫中的項目複製的項目有沒有辦法來防止這種情況? – Sandra 2012-03-29 16:32:04

+0

如果您的預加載數據較少,請在您的app.xaml.cs中使用第二種方法,否則在同一地點嘗試第一種方法 – 2012-03-29 19:05:47