Xamarin Android 数据访问Xamarin.Android Data Access

大多数应用程序都需要在本地将数据保存在设备上。除非数据量完全小,否则这通常需要应用程序中的数据库和数据层来管理数据库访问。 Android 具有 SQLite 数据库引擎 "内置",Xamarin 的平台简化了存储和检索数据的访问。本文档说明如何以跨平台的方式访问 SQLite 数据库。Most applications have some requirement to save data on the device locally. Unless the amount of data is trivially small, this usually requires a database and a data layer in the application to manage database access. Android has the SQLite database engine 'built in' and access to store and retrieve data is simplified by Xamarin's platform. This document shows how to access an SQLite database in a cross-platform way.

数据访问概述Data Access Overview

大多数应用程序都需要在本地将数据保存在设备上。Most applications have some requirement to save data on the device locally. 除非数据量完全小,否则这通常需要应用程序中的数据库和数据层来管理数据库访问。Unless the amount of data is trivially small, this usually requires a database and a data layer in the application to manage database access. Android 两者都具有 "内置" 的 SQLite 数据库引擎,并通过包含 SQLite 数据提供程序的 Xamarin 平台简化对数据的访问。Android both has the SQLite database engine "built in" and access to the data is simplified by Xamarin's platform which comes with the SQLite Data Provider.

Xamarin 支持数据库访问 Api,例如:Xamarin.Android support database access APIs such as:

  • ADO.NET 框架。ADO.NET framework.
  • SQLite-NET 第三方库。SQLite-NET 3rd party library.

本节中的大部分代码都是完全跨平台的,将在 iOS 或 Android 上运行,而无需修改。The majority of the code in this section is completely cross-platform and will run on iOS or Android without modification. 讨论了两个示例应用:There are two sample apps discussed:

  • DataAccess_Basic – 简单数据操作将结果写入文本显示控件;DataAccess_Basic – Simple data operations writes the results to a text display control;

  • DataAccess_Advanced – 将数据操作集成到一个小型工作应用程序中,该应用程序列出并编辑了简单的数据结构。DataAccess_Advanced – Integrates data operations into a small working application that lists and edits a simple data structure.

这两个示例解决方案都包含 iOS 和 Android 示例应用程序项目。Both sample solutions contain iOS and Android sample application projects.

对于 Xamarin. Forms 应用程序,请阅读使用数据库,其中介绍了如何使用 XAMARIN 在 PCL 库中处理 SQLite。For Xamarin.Forms applications, read working with databases which explains how to work with SQLite in a PCL library with Xamarin.Forms.

本部分中的主题介绍使用 SQLite 作为数据库引擎的 Xamarin 中的数据访问。The topics in this section discuss data access in Xamarin.Android using SQLite as the database engine. 可以通过使用 ADO.NET 语法来直接访问数据库,也可以在中C#包含 SQLite.NET ORM 并执行数据操作。The database can be accessed "directly" by using ADO.NET syntax or you can include the SQLite.NET ORM and perform data operations in C#.

评审了两个示例:一个示例包含非常简单的数据访问代码,该代码输出到一个文本字段,另一个是包含创建、读取、更新和删除功能的简单应用程序。Two samples are reviewed: one that contains very simple data access code that outputs to a text field, and a simple application that includes create, read, update and delete functionality. 此外,还讨论了如何使用预填充的 SQLite 数据库来为应用程序设定种子。Threading and how to seed your application with a pre-populated SQLite database is also discussed.

有关跨平台数据访问的其他示例,请参阅我们的Tasky Pro案例研究。For additional examples of cross-platform data access see our Tasky Pro case study.