(Windows Phone 8)Berkenalan Dengan Database Internal(LINQ)

dbwindowsphonedev
Selamat siang gan, tumben ya ane posting siang bolong gini, ya gini lah ane, kalo ada waktu luang insya allah semua akan ane bagikan. Untuk tema kita kali ini bermain dengan windows phone, banyak orang-orang bilang kalau smartphone besutan microsoft ini tidak semarak kompetitornya, tapi menurut ane semua itu sama, yang membedakan hanyalah fendor yang membuat, dan juga kebanyakan smartphone WP ini terlalu mahal untuk di miliki, tetapi ane lebih suka mengotak atik dalemannya dari pada memiliki langsung smartphone, karena ane hanya memikirkan HP yang hanya bisa telfon dan mengirim pesan itu sudah cukup, nah datang pertanyaan, Gimana ane debug aplikasi ane dan dapat berjalan dengan baik di smartphone nya langusung, Jawabannya singkat : ane mendebug app karya ane selalu menggunakan smartphone tmen ane, haa yang sekalian itung-itung berbagi aplikasi secara langsung dan memiplementasikan secara langsung kepada user(teman).

Data base internal merupakan data yang tersimpan langsung dalam memory smartphone, dengan contoh seperti contak, sms, memo yang ada pada aplikasi yang sudah di berikan default nya pada smartphone tersebut, nah itu juga yang akan kita bahas, di bawah ini merupakan hasil pencarian ane dengan beberapa referensi, semoga agan-agan dapat terbantu.

Language Integrated Query

(LINQ, pronounced “link”)

is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages, although ports exist for Java,[1] PHP, JavaScript and ActionScript.
LINQ extends the language by the addition of query expressions, which are akin to SQL statements, and can be used to conveniently extract and process data from arrays, enumerable classes, XML documents, relational databases, and third-party data sources. Other uses, which utilize query expressions as a general framework for readably composing arbitrary computations, include the construction of event handlers[2] or monadic parsers.[3]
LINQ also defines a set of method names (called standard query operators, or standard sequence operators), along with translation rules used by the compiler to translate query expressions into expressions using these method names, lambda expressions and anonymous types.
Many of the concepts that LINQ has introduced were originally tested in Microsoft’s Cω research project. LINQ was released as a major part of .NET Framework 3.5 on November 19, 2007.

pengimplementasian code

using System.Data.Linq;
using System.Data.Linq.Mapping;

 The complete code is as below,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace WP7_Samples
{    
    public partial class LocalDB_WP7 : PhoneApplicationPage
    {
        const string ConnectionString = "Data Source=isostore:/EmailDB.sdf";
        public LocalDB_WP7()
        {
            InitializeComponent();

            InputScope _scope = new InputScope();
            InputScopeName _scopeName = new InputScopeName();
            _scopeName.NameValue = InputScopeNameValue.EmailNameOrAddress;
            _scope.Names.Add(_scopeName);

            txtEmail.InputScope = _scope;

            using (var db = new EmailContext(ConnectionString))
            {
                if (!db.DatabaseExists())
                    db.CreateDatabase();

                LoadData();
            }
        }

        public void LoadData()
        {
            lstEmails.ItemsSource = GetEmails();
        }

        public IList GetEmails()
        {
            List emails = new List();
            using (var db = new EmailContext(ConnectionString))
            {
                var query = from e in db.Emails
                            select e;
                emails = query.ToList();
            }

            return emails;
        }

        public void AddEmail(EmailClass _email)
        {
            using (var db = new EmailContext(ConnectionString))
            {
                db.Emails.InsertOnSubmit(_email);
                db.SubmitChanges();
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (txtEmail.Text.Trim() != "")
            {
                var emailObj = new EmailClass() { EmailAddress = txtEmail.Text.Trim() };
                AddEmail(emailObj);
            }
            LoadData();
            txtEmail.Text = "";
        }
    }

    //Model Class
    [Table]
    public class EmailClass
    {
        [Column(IsDbGenerated = true, IsPrimaryKey = true)]
        public int Id { get; set; }
        [Column()]
        public string EmailAddress { get; set; }
    }

    public class EmailContext : DataContext
    {
        public EmailContext(string sConnectionString)
            : base(sConnectionString)
        { }

        public Table Emails
        {
            get
            {
                return this.GetTable();
            }
        }
    }
}

Selamat mencoba dan postingan selanjutnya kita akan membuat aplikasi yang menggunakan database internal secara langsung. Terima kasih dan semoga membatu agan-agan yang lagi haus teknologi.

Referensi :

http://en.wikipedia.org/wiki/Language_Integrated_Query
http://blogs.msdn.com/b/wriju/archive/2011/08/01/windows-phone-7-using-local-database-for-application.aspx

Advertisements

Published by

jarcode

hello I am jarcode

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s