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.