惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
B
Blog
M
MIT News - Artificial intelligence
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Proofpoint News Feed
C
Check Point Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
博客园 - 叶小钗
S
SegmentFault 最新的问题
Last Week in AI
Last Week in AI
T
The Blog of Author Tim Ferriss
Microsoft Security Blog
Microsoft Security Blog
小众软件
小众软件
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 【当耐特】
腾讯CDC
博客园 - Franky
博客园 - 聂微东
V
Visual Studio Blog
GbyAI
GbyAI
Martin Fowler
Martin Fowler
罗磊的独立博客
Y
Y Combinator Blog

博客园 - 天神一

Windows 8的本地化应用程序清单 代码滑动panorama-即程序中设置SelectedIndex WP7 真机调试PhotoChooserTask和CameraCaptureTask 解决windows8不能安装ZUNE的问题 软件概要设计 AutoResetEvent详解 解决VS2008 调试启动特别慢 微软WP7本地数据库之Sqlite编程技巧(转) 桥接模式的简单分析 代码的“门面”——模式系列谈之Facade模式(转) SilverLight4:在MVVM架构下实现模式窗口 简析SilverLight的List<T>、ObservableCollection<T>和INotifyPropertyChanged的异同 MVVM架构的简单解析 在MVVM架构下,把EventArgs绑定到Command上【翻译】 XNA 4.0新书:初学者很好的书籍 SilverLight:在MVVM中实现多事件 SilverLight:使用MVVM实现View层在程序运行时自动生成控件并且取得其值 (翻译)在MVVM模式中打开子窗体(Child Window) Windows Phone 7 完全版电子书可以免费下载了
WP7开发 Sqlite数据库的使用 解决Unable open the database
天神一 · 2013-07-22 · via 博客园 - 天神一

WP7本身不支持Sqlite数据库,但我们可以添加第三方组件让它支持Sqlite. 首先在项目中添加引用Community.CsharpSqlite.WP.dll,我会放后面让大家下载,我下了有几天了,是源码,我也找不回原网址了,所以就编译了一下,直接引用就可以了. 另外,原版在使用从外部添加的数据库时(即不是在程序中生成的数据库),会提示”Unable open the database”,我改了一下,已经解决该问题.经测试,不支持like语法,就是不支持模糊查询,这个有点郁闷。 解决方法:打开os_win_c.cs,找到第795行:

1
pFile.fs = new IsolatedStorageFileStream(zConverted, dwCreationDisposition, dwDesiredAccess, dwShareMode, store);

替换为:

1
pFile.fs = new IsolatedStorageFileStream(zConverted, FileMode.OpenOrCreate, dwDesiredAccess, dwShareMode, store);

下面说说具体的使用方法,只详细介绍select读取数据,其他的没有返回,很简单. 假设数据库为db.db,表为station,字段为station_name. 执行Sql后,返回的是一个枚举类型,我们要先定义一个存放返回数据的类:

1
2
3
4
5
6
7
8
9
10
11
12
13
    public class Train
    {
        public Train() { }
 
        string _name;
        public string station_name
            //属性名必须与列名相同
        {
            get { return _name; }
            set { _name = value; }
        }
 
    }

因为只读station_name,所以只有一个属性. 读取数据库并显示在ListBox上:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
            SQLiteConnection db = new SQLiteConnection("db.db");
            //连接数据库,如果不存在,会自动创建,我事先已经拷了一个进去,所以可以下面的select操作
            //数据库是使用IsolatedStorageFile存取的,可以使用IsolatedStorageFile复制,删除等
            db.Open();
            //打开数据库
            SQLiteCommand cmd=db.CreateCommand("select station_name from station limit 100");
            IEnumerable<Train> lst = cmd.ExecuteQuery<Train>();
            //返回Train类型的枚举
            //非select语句,使用cmd.ExecuteNonQuery();
            List<string> s = new List<string>();
 
            foreach (Train o in lst)
            {
                s.Add(o.station_name);
            }
            listBox1.ItemsSource = s;
            //显示在ListBox上
 
            db.Dispose();
            db = null;
            //释放资源

ListBox的SelectedItem属性返回的是Train类的实例,但需要强制转换:

1
Train train=(Train)listBox1.SelectedItem;