12 Eylül 2012 Çarşamba

Windows Phone Local Database Uygulamasi


Windows phone uzerinde Local Database mantigini kullanarak basit bir alis veris listesi uygulamasi yapalim. Listemize ekledigimiz maddeleri Windows Phone uzerinde bir veritabani olusturup bu veritabani uzerinde eklemis oldugumuz maddeleri sakliyor olacagiz. Bu islemleri yapabilmemiz icin uygulamamiz uzerinde DataContext ve LinqToSqlyapilarini kullaniyor olacagiz.


Windows phone uzerindeki yerel veritabaninizda  verileri saklayabilmek ve verileri cekebilmek icin Linq to sql kullanilir. Linq To Sql sayesnde verilerimiz ile calisabilmemizde bir nesneye yonelimli bir yaklasim saglayarak nesne modeli gorevini olusturmaktadir. System.Data.Linq.DataContext nesnesinin bizlere saglamis oldugu model sayesinde LinqToSql Runtime ile Isolated Storage’de bulunan yerel veritabanini uygulama icerisindeki datacontext ile iletisimi saglayacak ve veriler uzerinde calisabilecektir.
Simdi dilerseniz bu yapilari kullanarak basit bir Alis Veris Listesi uygulamasi yapalim.











Alis veris listemize adet ve alinacak ile ilgili madde hakkinda bilgileri girdikten sonra Alis Veris Listesine Ekle butonunu tikliyoruz. Eklemis oldugumuz listeyi uygulamadan cikmis olsak bile artik o listemiz isolated storage’de bulunan yerel veritabanimiz icerisinde verilerimiz saklaniyor olacaktir. Listenizden maddeleri cikarmak istiyorsaniz ise Secili Olanlari Listeden Kaldir butonunu kullaniyor olacagiz.
Dilerseniz artik kod kismina gecelim.

MainPage.Xaml Kaynak Kodlari ;

<phone:PhoneApplicationPage
x:Class=”AlisVeris_App.MainPage”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
xmlns:phone=”clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone”
xmlns:shell=”clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone”
xmlns:d=”http://schemas.microsoft.com/expression/blend/2008″
xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″
mc:Ignorable=”d” d:DesignWidth=”480″ d:DesignHeight=”768″
FontFamily=”{StaticResource PhoneFontFamilyNormal}”
FontSize=”{StaticResource PhoneFontSizeNormal}”
Foreground=”{StaticResource PhoneForegroundBrush}”
SupportedOrientations=”Portrait” Orientation=”Portrait”
shell:SystemTray.IsVisible=”True”>
<Grid x:Name=”LayoutRoot” Background=”Transparent”>
<Grid.RowDefinitions>
<RowDefinition Height=”Auto”/>
<RowDefinition Height=”*”/>
</Grid.RowDefinitions>
<StackPanel x:Name=”TitlePanel” Grid.Row=”0″ Margin=”12,17,0,28″>
<TextBlock x:Name=”ApplicationTitle” Text=”Alış Veriş Listesi” Style=”{StaticResource PhoneTextNormalStyle}”/>
<TextBlock x:Name=”PageTitle” Text=”Alış Veriş App” Margin=”9,-7,0,0″ Style=”{StaticResource PhoneTextTitle1Style}”/>
</StackPanel>
<Grid x:Name=”ContentPanel” Grid.Row=”1″ Margin=”12,0,12,0″ Loaded=”ContentPanel_Loaded”>
<ListBox Height=”347″ HorizontalAlignment=”Left” Margin=”16,175,0,0″ Name=”listBox1″ VerticalAlignment=”Top” Width=”426″ />
<Button Content=”Alis Veris Listesine Ekle” Height=”70″ HorizontalAlignment=”Left” Margin=”9,0,0,438″ Name=”button1″ VerticalAlignment=”Bottom” Width=”441″ Click=”button1_Click” />
<Button Content=”Secili Olanlari Listeden Kaldir” Height=”70″ HorizontalAlignment=”Left” Margin=”1,0,0,6″ Name=”button2″ VerticalAlignment=”Bottom” Width=”441″ Click=”button2_Click” />
<TextBox Height=”73″ HorizontalAlignment=”Right” Margin=”0,38,14,0″ Name=”textBox1″ Text=”" VerticalAlignment=”Top” Width=”357″ />
<TextBox Height=”73″ HorizontalAlignment=”Left” Margin=”6,38,0,0″ Name=”textBox2″ Text=”" VerticalAlignment=”Top” Width=”93″ />
<TextBlock Height=”27″ HorizontalAlignment=”Left” Margin=”99,16,0,0″ Name=”textBlock1″ Text=”Alınacak” VerticalAlignment=”Top” Width=”291″ />
<TextBlock Height=”34″ HorizontalAlignment=”Left” Margin=”18,16,0,0″ Name=”textBlock2″ Text=”Adet” VerticalAlignment=”Top” Width=”116″ />
</Grid>
</Grid>
</phone:PhoneApplicationPage>

AlisVeris.Cs Kaynak Kodlari ;

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data.Linq.Mapping;
using Microsoft.Phone.Data.Linq;
namespace AlisVeris_App.Model
{
[Table(Name = "AlisVeris")]
public class AlisVeris
{
   [Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int ID
{
get;
set;
}
  [Column(CanBeNull = false)]
public string Alinacak
{
get;
set;
}
public override string ToString()
{
return string.Format(“{0}”,  this.Alinacak);
}
}
}

AlisVerisDataContext.Cs Kaynak Kodlari ;

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data.Linq;
namespace AlisVeris_App.Model
{
public class AlisVerisDataContext : DataContext
{
public AlisVerisDataContext(string connectionString)  : base(connectionString)
{
}
public Table<AlisVeris> AlisVeris
{
get
{
return this.GetTable<AlisVeris>();
}
}
}
}

MainPage.Xaml.Cs Kaynak Kodlari ;

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 Microsoft.Phone.Data.Linq;
using AlisVeris_App.Model;
namespace AlisVeris_App
{
public partial class MainPage : PhoneApplicationPage
{
   private const string ConnectionString = @”isostore:/AlisVerisDB.sdf”;
public MainPage()
{
InitializeComponent();
}
public void AlisVerisYazdir()
{
   using (AlisVerisDataContext context = new AlisVerisDataContext(ConnectionString))
            {
                this.AlisVerisYazdir(context);
            }
}
public void AlisVerisYazdir(AlisVerisDataContext context)
{
AlisVeris av = new AlisVeris()
{
Alinacak = string.Format(“{0} Adet {1}  “, textBox2.Text, textBox1.Text),
};
context.AlisVeris.InsertOnSubmit(av);
            context.SubmitChanges();
}
public IEnumerable<AlisVeris> AlisVerisOku()
{
IEnumerable<AlisVeris> av = null;
using (AlisVerisDataContext context = new AlisVerisDataContext(ConnectionString))
            {
                av = context.AlisVeris.ToList();
            }
return av;
}
private void button1_Click(object sender, RoutedEventArgs e)  //Veri Ekleme
{
using (AlisVerisDataContext context = new AlisVerisDataContext(ConnectionString))
{
  if (!context.DatabaseExists())
                {
                    context.CreateDatabase();
                    this.AlisVerisYazdir(context);
                }
else
{
        DatabaseSchemaUpdater schemaUpdater = context.CreateDatabaseSchemaUpdater();
int version = schemaUpdater.DatabaseSchemaVersion;
if (version == 0)
{
this.AlisVerisYazdir(context);
schemaUpdater.Execute();
}
}
}
this.listBox1.ItemsSource = this.AlisVerisOku();
textBox1.Text = “”;
textBox2.Text = “”;
}
private void ContentPanel_Loaded(object sender, RoutedEventArgs e)
{
  using (AlisVerisDataContext context = new AlisVerisDataContext(ConnectionString))
{
if (context.DatabaseExists())
{
this.listBox1.ItemsSource = this.AlisVerisOku();
}
else
{ this.listBox1.Items.Clear(); }
}
}
private void button2_Click(object sender, RoutedEventArgs e)  //Veri Silme
{
try
{
using (AlisVerisDataContext context = new AlisVerisDataContext(ConnectionString))
{
     IQueryable<AlisVeris> alverSorgu = from av in context.AlisVeris
                                                       where av.Alinacak == this.listBox1.SelectedItem.ToString()
                                                       select av;
                    AlisVeris alverRemove = alverSorgu.FirstOrDefault();
                    context.AlisVeris.DeleteOnSubmit(alverRemove);
                    context.SubmitChanges();
this.listBox1.ItemsSource = this.AlisVerisOku();
}
}
catch (Exception ex)
{
MessageBox.Show(“Lütfen sileceğiniz maddeyi seçiniz…”);
}
}
}
}
Keyifli Calismalar Dilerim…

Hiç yorum yok:

Yorum Gönder