Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now that we've cloned our sonnets plus flat file solution into a sonnets plus db solution. Let's talk about the overall architecture of reading and writing to the database. Microsoft, on the Microsoft developer network or MSDN, has a great article called Local Database for Windows Phone. And there's lots of nuanced detail in here, which you'll probably want to review after you complete this section. What we're going to talk about right now is just the architectural considerations related to how we're going to use the database and hook it up to our lists which are done with XML data binding. The way that that's connected, you can see here in the right hand box inside isolated storage you can now have SDF files.
Which are files where the SQL CE databases are contained. I previously mentioned that if you are targeting only Windows Phone 8. You should use the API's in the Windows.Storage namespace. However, if you are using SQL CE that still requires using isolated storage. The arrow in the middle there says that you use use LINQ to SQL to talk to the database. And the way that you use LINQ to SQL is the System.Data.Linq name space. Which has a data context object that implements the same kind of data context functionality required for the XML data binding.
What we're going to do is add a reference to the system.data.linq assembly. And then we're going to go ahead and start converting the bits and pieces of our Sonnets Plus DB app to use the database. So now let's open Visual Studio and get started. The first order of business is to open the Solution explorer. Open up the references and add a reference to system.data.linq, because that's what we need in order to be able to talk to the database. Come down there, there we are. System.data.linq. Now the next thing we're going to do is go to our sonnets class.
Now this is our sonnets class from the flat file version. You see we simply have a collection of properties that we used to read and write data from the XML. When we convert that to the database version. It looks very similar except that there are a number of attributes associated with each item. First of all we edited a couple of additional using statements in order to be able to bring in the assemblies that support these attributes. And then this is the equivalent of the DDL to define the structure of the database. We start out by saying sonnets is a table in the database, and we define each one of our properties as a column. Now because this is a database then we want to be able to do queries against it. We also want to have a primary key.
And so this collection of stuff right here, is all the magic link stuff that says that we want this column Sonnet ID to be a primary key. We want the database to generate it for us. We want the database type to be integer not null, and we want it to be an identity column. Which means it's gotta be a unique value. We said can be null equal to false, because there's no point in having a primary key that's null because we can't search on it. And then we say auto sync equals auto sync.on insert.
Which means that when we insert something, that's when the initial value of the primary key is generated. But beyond that, our properties look the same, other than they say that they are now columns in the database. So this is, in terms of database setup, all that we need to do. We don't need to open up SQL server explorer and go through wizards and dialog boxes and grids in order to be able to create the data. Especially because this data is being created in the SQL CE database on the phone inside your apps' private isolated storage.
We save that. And now we need to be able to connect them. That table definition to the database using a database context class. So we'll come back to the solution explorer and add a new class. And we'll call it the Sonnets DB data context and we fill that in. Here's what we got. We got the (UNKNOWN) statements, which are similar to what we had before. We had system.data.linq and system.data.linq.mapping and we have a class which implements the system.data.linq data context. It only has two things in it, ome of them is, is a declaration of a property which represents the table.
Which is a type, table of T. And that specifies items so that we can use the existing data binding because we're going to be using items. And the constructor, which is because this is a system.data.linq data context also has to have a connection string. And we have to pass the connection string to the base class during construction. Now the item view model is going to remain the same. But we need to make a few more changes to the app.xml.cs in order to be able to support the initial process of using the database.
So here's where we're going to actually start accessing the database. Now let's take a look what we got. If you remember, back in the previous version, we had a DB name property. Well, that's gone now. We have replaced that with a DB connection string property. And since this is actually SQL, it needs to say datasource equals and then you see here isostore colon slash. So this is a specific string format that tells the database that the connection is in a database and in the isolated storage.
And, in our case, we called it sonnets2.sdf. Whatever databases you create have to have a file extension of sdf. Then in order to be able to connect to this data contacts from other parts of the application. We define a private incidence variable of type Sonnets DB Data context which we just looked at. And a property getter and setter that allows us to know that we need to actually do the construction the first time we access this and pass on the data base Connection String. If we look at the rest of app.xml cs most of it is the same as we've seen before. Except at the beginning of the app constructor, we initialize a variable of type sonnets db data context.
And call our property in order to actually instantiate the data base and we'll use it down here a little bit further on. And just as we did before, we need to determine whether the database has been pre-populated by the Shakespeare sonnets dot xml file. Here, unlike in sonnets plus flat file, we use the database data context in order to be able to query whether the database exists. Rather than, the isolated storage manager, and trying to see whether the file exists. So we first ask the system.linq.datacontext whether the data base exists.
If it doesn't exist, then we create it with this db dot create data base that does introspection over the Table class and actually creates the table and the data base with all the appropriate types. Then Just as we did before, and this method hasn't changed. Our load database from XML, which reads the Shakespeare sonnets.XML and creates an observable collection of item view model. And then, instead of taking that and writing it out as a serialized XML file. We're going to iterate over the collection of item view model instances And ask the database to insert the items. But it inserts them on submit.
So, what happens here, as we say. Database.items.insert on submit. And we create a new instance of that sonnets class that has the data that we want. The one that was annotated by the attributes that describe the columns and their data types. You notice because we are using the default values, we don't set the sonnet ID, that's the primary key which is auto generated. And we don't need to set the is favorite value because it defaults to false.
So, we say DB and items on insert submit, we create a new instance of sonnets, fill it in. And when we are all done with that then we say DB.submit changes. And that actually takes all of those objects that were in the observial collection and writes them out to the date base. As always depending upon the size of the data your dealing with and the amount of memory in the device that you're dealing with. It works great for 154 sonnets. If you had ten thousand US census data items, you probably would want to call db.SubmitChanges earlier than at the end of calling InsertOnSubmit ten thousand times.
So now we have the database set up. We've got our sonnets class with the proper attributes, we've got our DBDataContext class all set up and we've seen the first incidents of accessing and writing data to the database.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99368 Viewers
56 Video lessons · 112625 Viewers
71 Video lessons · 81446 Viewers
131 Video lessons · 39099 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.