简体   繁体   English

Windows Phone 7.5 Mango应用程序上的预加载数据库

[英]Preloaded Database on Windows Phone 7.5 Mango Application

Im creating an App in visual studio for Windows Phone Mango in which I need a dictionary database. 我在Visual Studio中为Windows Phone Mango创建了一个应用程序,我需要一个词典数据库。 I know how to create it, how to insert, update and delete. 我知道如何创建,如何插入,更新和删除。

I have a 300k xml file of data which I want to preload in that database (parse and insert - thats not the problem) into the application. 我有一个300k的xml数据文件,我希望将该数据库预加载到应用程序中(解析并插入-多数民众赞成在这不是问题)。 The problem is that I don't want to do it when the app is being installed. 问题是在安装应用程序时我不想这样做。 I want the database file preinstalled in the isolated data storage. 我希望将数据库文件预安装在隔离的数据存储中。 I also need that system on-board because the xml contents could change every 3 months. 我还需要板载该系统,因为xml内容可能每3个月更改一次。 But processing a 300k xml file on a phone - every time when the app is installed - sounds quite... stupid when I can make the database preinstalled... 但是,每次安装该应用程序时,在手机上处理300k xml文件听起来都非常……当我可以预先安装数据库时,这很愚蠢……

PS. PS。 Is there any decent XML helpers in linq? linq中是否有任何不错的XML帮助器? For caching xml files for example.. 例如,用于缓存xml文件。

This doesn't address your situation with XML helpers for Linq, but a good way to handle this is to create the database separately (like in a Console Application project) and load it up with the data you need. 这不能通过Linq的XML帮助器解决您的情况,但是处理此问题的一种好方法是分别创建数据库(例如在Console Application项目中)并使用所需的数据加载数据库。 Next, copy the SDF file into your Windows Phone project as Content. 接下来,将SDF文件作为内容复制到Windows Phone项目中。 Lastly, when you deploy your application, copy the database out of the deployment location into isolated storage. 最后,在部署应用程序时,将数据库从部署位置复制到隔离的存储中。 That way, you don't have to deal with XML files, or anything like that. 这样,您就不必处理XML文件或类似的文件。 It is a simple matter to copy from the deployment location to Isolated Storage. 从部署位置复制到隔离存储很简单。 I am working on a (quite long) blog post that talks all about this, but the core code you need is this: 我正在写一篇(很长的)博客文章,讨论所有这些,但是您需要的核心代码是:

// 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("ReferenceDB.sdf", UriKind.Relative)).Stream)
{
    // Create a stream for the new file in isolated storage.
    using (IsolatedStorageFileStream output = iso.CreateFile("ApplicationDB.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);
        }
    }
}

Then you can connect to the database as normal and read/write data all day long: 然后,您可以正常连接到数据库并整日读取/写入数据:

var db = new MyDataContext("Data Source = 'isostore:/ApplicationDB.sdf';");

You can download some sample code from my GitHub repository at https://github.com/ChrisKoenig/ReferenceDatabase 您可以从我的GitHub存储库中下载一些示例代码, 网址https://github.com/ChrisKoenig/ReferenceDatabase

-Chris -克里斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM