|
本文算是整理,了ArcGIS_Mobile 和 锦燕云 的博文: 地址: http://blog.csdn.net/arcgis_mobile/article/details/7597519
准备工作: 下载压缩包
1. 创建数据库
第一步:新建 Windows Phone应用程序,目标平台选择 wpos 7.1 (我给项目取名为TestSQLite)

第二步:添加压缩包中 Community.CsharpSqlite.WP.dll 的引用:
(引用,单击鼠标右键-->添加引用...)

第三步: 添加四个按钮 : 注意命名(btnOpen 创建并打开数据库 ,btnPopulate 创建表 ,btnClear 清空数据,btnClose,关闭连接)
 - 第四步: 添加对SQLite的引用: using SQLiteClient;
- 第五步: 添加SQLite数据库连接变量:
- SQLiteConnection mySQLiteDB = null;
- [color=#0000ff]public[/color] [color=#0000ff]partial[/color] [color=#0000ff]class[/color][color=#000000] MainPage : PhoneApplicationPage { SQLiteConnection mySQLiteDB [/color]= [color=#0000ff]null[/color][color=#000000]; [/color][color=#008000]//[/color][color=#008000] 构造函数[/color] [color=#0000ff]public[/color][color=#000000] MainPage() { InitializeComponent(); }[/color]
复制代码 第六步: 给“Open”按钮添加事件,创建并打开数据库:Open按钮点击事件- Open按钮点击事件
- private void btnOpen_Click(object sender, RoutedEventArgs e)
-
- {
-
- if (mySQLiteDB == null)
-
- {
-
- mySQLiteDB = new SQLiteConnection("TestSQLiteDB");
-
- mySQLiteDB.Open();
-
-
-
- btnOpen.IsEnabled = false;
-
- btnClose.IsEnabled = true;
-
- btnClear.IsEnabled = false;
-
- btnPopulate.IsEnabled = true;
-
- }
-
- }
复制代码 第七步:创建表,并往表中填充数据:- private void btnPopulate_Click(object sender, RoutedEventArgs e)
-
- {
-
- //创建表RegisteredStudents,有3个属性:id、姓名、学号
-
- SQLiteCommand cmd = mySQLiteDB.CreateCommand("Create table RegisteredStudents (id int primary key,name text,zipcode numeric(7))");
-
- int i = cmd.ExecuteNonQuery();
-
- int id = 0;
-
- string name = "Name" + id;
-
- int zipcode = 98000;
-
- for (int j = 0; j < 10; j++)
-
- {
-
- id++;
-
- name = "Name" + id;
-
- zipcode = 98000 + id;
-
- cmd.CommandText = " Insert into RegisteredStudents (id, name, zipcode) values (" + id +","" + name + ""," + zipcode +")";
-
- i = cmd.ExecuteNonQuery();
-
- }
-
-
-
- btnPopulate.IsEnabled = false;
-
- btnClear.IsEnabled = true;
-
- }
复制代码 第八步:清空表中的数据:- private void btnClear_Click(object sender, RoutedEventArgs e)
-
- {
-
- SQLiteCommand cmd = mySQLiteDB.CreateCommand("drop table RegisteredStudents");
-
- int i = cmd.ExecuteNonQuery();
-
-
-
- btnPopulate.IsEnabled = true;
-
- btnClear.IsEnabled = false;
-
- }
复制代码 第九步: 断开数据库连接,关闭数据库:- private void btnClose_Click(object sender, RoutedEventArgs e)
-
- {
-
- if (mySQLiteDB != null)
-
- {
-
- mySQLiteDB.Dispose();
-
- mySQLiteDB = null;
-
- btnOpen.IsEnabled = true;
-
- btnPopulate.IsEnabled = false;
-
- btnClear.IsEnabled = false;
-
- btnClose.IsEnabled = false;
-
- }
-
- }
复制代码 运行程序,点击open可以在WP的模拟器的独立存储空间中创建名为“TestSQLiteDB”数据库,点击populate按钮可以为其填充数据,点击clear可以清空数据库中的数据,close关闭数据库连接; 2.获取数据库文件,
第一步: 直接双击 WindowsPhonePowerTools.application 文件,

稍等片刻后出现以下界面, 有三个选项,选择(512M这一项) , 真机就选择第一项,之后点击CONNECT

第二步: 点击CONNECT后:出现以下页面点击BROWSE按钮,选择你创建的这个Windows Phone项目下的xap文件
(既是编译之后,Debug下的xap,例如我的XAP位置: C:\Users\RY\Documents\Visual Studio 2010\Projects\FirstWP\TestSQLite\Bin\Debug)

第三步: 再点击 INSTALL,稍等片刻,这个地方,我2 了一把,以为点击INSTALL就OK 了,
实际上是要点击 :
,
之后出现下图:.....O(∩_∩)O哈哈~ 你看到了吧-->TestSQLite,

点开就能看到数据库了...就是这个TestSQLiteDB,点击 GET 按钮, 将这个数据库文件保存到桌面 (注意,如果你还处于调试状态,此时要停止调试,否则会报错,..),

3.操作数据库
安装压缩包中的 SQLiteManagerSetup 工具
安装好后,双击,后点击 Use Demo -->Continue,找到保存在桌面的TestSQLiteDB,打开:

以上,这就是你创建的数据库,现在你可以操作它了....
本文来自RY一步一个脚印的博客,原文地址:http://www.cnblogs.com/ry123/archive/2012/05/31/2528281.html
|
|