Correct and simple use of SQLite database in monotouch

Source: Internet
Author: User

IOS provides SQLite as a local database, and monotouch also provides mono. Data. SQLite to encapsulate SQLite. Compared with objective-C using SQLite databases, it is easy to use monotouch to access SQLite data. First, let's look at the class libraries provided by mono. Data. SQLite. There are several important classes:

  • Sqliteconnection, inherited from system. Data. Common. dbconnection;
  • Sqlitecommand, inherited from system. Data. Common. dbcommand;
  • Sqlitedataadapter, inherited from system. Data. Common. dbdataadapter;
  • Sqlitedatareader, inherited from system. Data. Common. dbdatareader;
  • Sqlitefactory, inherited from system. Data. Common. dbproviderfactory;
  • Sqliteparameter, inherited from system. Data. Common. dbparameter;
  • Sqlitetransaction, inherited from system. Data. Common. dbtransaction;

If you are not familiar with these classes starting with SQLite, you can use the system. data. the classes under common should be too familiar. That's right. This is the standard ADO. net, so as long as ADO. NET developers can get started almost immediately, which is exactly what monotouch is. the value provided by NET developers. Let's take a look at the following code:

using (var connection = SqliteFactory.Instance.CreateConnection()) {   connection.ConnectionString = string.Format("data source={0}/northwind.db3; version=3;", Environment.CurrentDirectory);   var command = connection.CreateCommand();   command.Connection = connection;   command.CommandText = "SELECT * FROM Products WHERE CategoryID = ?";   var parameter = command.CreateParameter();   parameter.Value = "1";   command.Parameters.Add(parameter);   connection.Open();   var reader = command.ExecuteReader(CommandBehavior.CloseConnection);   while (reader.Read()) {      // use reader's data here ...   }}

This code is very common. Apart from using a sqlitefactory, it is almost no different from ado.net data access. However, from this code, we can see that, the original code of our project can be compiled with monotouch, And I believe many people have their own encapsulation of ado.net. If monotouch exists, these packages can be easily transplanted to monotouch.

Finally, I have to say,. NET developers, monotouch is really a good thing, it allows you to join iOS development as soon as possible, to maximize your. net skills extend to IOS.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.