Using Local Database for Application
9758 단어 application
using System.Data.Linq;
using System.Data.Linq.Mapping;
After that you need to define the model by hand.
//Model Class
[Table]
public class EmailClass
{
[Column(IsDbGenerated = true, IsPrimaryKey = true)]
public int Id { get; set; }
[Column()]
public string EmailAddress { get; set; }
}
Once the model is defined you then need the DataContext
public class EmailContext : DataContext
{
public EmailContext(string sConnectionString)
: base(sConnectionString)
{ }
public Table<EmailClass> Emails
{
get
{
return this.GetTable<EmailClass>();
}
}
}
Connection string would look like,
string ConnectionString = "Data Source=isostore:/EmailDB.sdf";
Some reusable methods to Add and Display
public IList<EmailClass> GetEmails()
{
List<EmailClass> emails = new List<EmailClass>();
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();
}
}
Then comes UI which would help you to save and display data,
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<TextBox Width="300" x:Name="txtEmail"></TextBox>
<Button HorizontalAlignment="Right" x:Name="btnSave" Content="Save" Width="120" Click="btnSave_Click"></Button>
</StackPanel>
<ListBox x:Name="lstEmails" Grid.Row="1" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=EmailAddress}"></TextBlock>
<TextBlock Text="-----"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
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<EmailClass> GetEmails()
{
List<EmailClass> emails = new List<EmailClass>();
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<EmailClass> Emails
{
get
{
return this.GetTable<EmailClass>();
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Pre-Query SamplesValidate the current query criteria or provide additional query criteria programmatically, just before sending the SELEC...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.