积极答复者
Windows Phone 7.1 / 8 如何嵌入第三方数据库文件?

问题
-
hi all,
是这样的,我打算用sqlite作为App的数据库,首先因为我有一些初始数据在数据库里面,所以我想把数据库打包进安装包里面,这样程序安装好后就可以直接使用,但是,程序安装好后访问不到那个数据库,必须重新创建一个新的数据库才能访问到那个新的数据库,请问这个是什么原因?我应该怎么才能访问到安装包的那个数据库?
- 已编辑 fly-studio 2013年5月11日 4:15
答案
-
一般思路是你先把sqlite数据库文件放到项目根目录下,然后在你程序要使用这个数据库之前,把它拷贝到程序的本地文件夹下,建议调用下面函数:
sr为你的sqlite数据库的流,fn是你拷贝到程序本地文件夹下的数据库文件路径
public static void MoveSQLiteDBToIsoStore(StreamResourceInfo sr,string fn)
{
IsolatedStorageFile iStorage = IsolatedStorageFile.GetUserStoreForApplication();
if (!iStorage.FileExists(fn))
{
//程序第一次运行把SQLite数据库Copy到本地存储
using (var outputStream = iStorage.OpenFile(fn, FileMode.CreateNew))
{
byte[] buffer = new byte[10000];
for (; ; )
{
int read = sr.Stream.Read(buffer, 0, buffer.Length);if (read <= 0)
break;outputStream.Write(buffer, 0, read);
}
}
}
}这样的话程序要访问数据库数据的话,就能通过访问本地文件夹的数据库来进行操作了
这里wp平台使用sqlite数据库,有以下第三方库可以使用:
vici.core.wp7,vici.coolstorage.wp7,community.csharpsqlite.sqliteclient.wp7,community.csharpsqlite.wp7
这里我还是建议用自带的sqlce数据库
最后有个问题就是,如果拷贝数据库成功到程序的本地文件夹下后,能不能有什么思路可以把程序项目根目录下的那个sqlite数据库文件杀掉呢,求思路...
- 已标记为答案 fly-studio 2014年11月7日 10:29
全部回复
-
一般思路是你先把sqlite数据库文件放到项目根目录下,然后在你程序要使用这个数据库之前,把它拷贝到程序的本地文件夹下,建议调用下面函数:
sr为你的sqlite数据库的流,fn是你拷贝到程序本地文件夹下的数据库文件路径
public static void MoveSQLiteDBToIsoStore(StreamResourceInfo sr,string fn)
{
IsolatedStorageFile iStorage = IsolatedStorageFile.GetUserStoreForApplication();
if (!iStorage.FileExists(fn))
{
//程序第一次运行把SQLite数据库Copy到本地存储
using (var outputStream = iStorage.OpenFile(fn, FileMode.CreateNew))
{
byte[] buffer = new byte[10000];
for (; ; )
{
int read = sr.Stream.Read(buffer, 0, buffer.Length);if (read <= 0)
break;outputStream.Write(buffer, 0, read);
}
}
}
}这样的话程序要访问数据库数据的话,就能通过访问本地文件夹的数据库来进行操作了
这里wp平台使用sqlite数据库,有以下第三方库可以使用:
vici.core.wp7,vici.coolstorage.wp7,community.csharpsqlite.sqliteclient.wp7,community.csharpsqlite.wp7
这里我还是建议用自带的sqlce数据库
最后有个问题就是,如果拷贝数据库成功到程序的本地文件夹下后,能不能有什么思路可以把程序项目根目录下的那个sqlite数据库文件杀掉呢,求思路...
- 已标记为答案 fly-studio 2014年11月7日 10:29