WindowsPhone 7 本地数据库
前言:不懂数据库,感觉真痛苦,想但年我们玩C的时候,只能通过输入流,输出流来存储文件...苦逼的年代,不过现在有了SQL,MySQL,方便很多了。玩数据库玩多了,接触windowsphone开发,很自然地就问,wp的数据怎么存储啊?最后经过个各种途径,终于明白windowsphone 7有一下两种数据存储方式:
1. 本地存储——IsolatedStorageFile ,这种就是,类似于C语言的输入流和输出流的存储机制,简单的步骤如下:
a: 获取程序的独立存储空间(这里涉及到windowsphone的文件存储机制:每个程序的存储空间都会在逻辑上独立于其他,所以,在你看法程序的同时,你已经有自己程序独立的存储空间Storage了),而这里的获 取,也就是获取你的存储空间,这样做不会影响其他程序。 【IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication() //这里就可以获取到自己的空间file了,之后就可以对file操作】
b.然后就可以在自己程序的目录下创建各种目录和文件。【 file.CreateDirectory("MyDb");】
c.创建用于读写操作独立空间的文件流。【 IsolatedStorageFileStream streamMoney = new IsolatedStorageFileStream("MyDb\\Money.txt", System.IO.FileMode.Open, file);】
d.创建输入流和输出流,最后记得关闭。
【 StreamReader readMoney = new StreamReader(streamMoney);
money += readMoney.ReadLine(); //从当前流中读取一行字符返回
readMoney.Close();
streamMoney.Close(); 】
2.本地数据库——Local Database,有自己的数据库文件: isostore:/Money.sdf,机制大概是这样的:先创建自己的数据类MyMoney.cs,使数据类映射到对应的表;继承DataContext(数据库代理的对象,包含数据库中的Table对象和我们所说的链接字符串);建立数据库MyDB。
Demo:
1.MyMoney.cs,一下高亮的不能漏掉,否则映射会失败。
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;
using System.ComponentModel;
using System.Data.Linq.Mapping;
namespace DataBoundApp.DataSource
{
[Table]
public class MyMoney :INotifyPropertyChanged,INotifyPropertyChanging
{
private int _index;
[Column(IsPrimaryKey = true, CanBeNull = false, IsDbGenerated = true, DbType = "INT NOT NULL Identity", AutoSync = AutoSync.OnInsert)] // 自动递增的IsPrimaryKey
public int Index
{
get
{
return _index;
}
set
{
if (_index != value)
{
NotifyPropertyChanging("Index");
_index = value;
NotifyPropertyChanged("Index");
}
}
}
private string _in_or_out;
[Column]
public string In_Or_Out
{
get
{
return _in_or_out;
}
set
{
if (_in_or_out != value)
{
NotifyPropertyChanging("In_Or_Out");
_in_or_out = value;
NotifyPropertyChanged("In_Or_Out");
}
}
}
private string _msg;
[Column]
public string Msg
{
get
{
return _msg;
}
set
{
if (_msg != value)
{
NotifyPropertyChanging("Msg");
_msg = value;
NotifyPropertyChanging("Msg");
}
}
}
private string _money;
[Column]
public string Money
{
get
{
return _money;
}
set
{
if (_money != value)
{
NotifyPropertyChanging("Money");
_money = value;
NotifyPropertyChanged("Money");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangingEventHandler PropertyChanging;
private void NotifyPropertyChanging(string propertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
}
}
2.MyDataContext.cs
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 DataBoundApp.DataSource
{
public class MyDataContext:DataContext
{
public Table<MyMoney> tables;
public MyDataContext(string StrConnectiong)
: base(StrConnectiong)
{
}
}
}
3.建议在程序开始之前就创建好数据库,在App.xaml.cs 中
private static string connectionstring = " isostore:/Money.sdf";
public static MyDataContext myDB = new MyDataContext(connectionstring);
private void InitDB()
{
if (!myDB.DatabaseExists())
{
myDB.CreateDatabase();
//myDB.tables.in
}
}
这样,一个数据库Money.sdf就创建好了,之后就可以对数据库操作了。附上关键代码:
private void AddMoneyBtn_Click(object sender, RoutedEventArgs e)
{
if (MtextBox.Text.Trim() == "" || StextBox.Text.Trim() == "")
return;
else
{
MyMoney mymoney = new MyMoney();
mymoney.In_Or_Out = @"+" + MtextBox.Text.Trim() + "¥";
mymoney.Msg = StextBox.Text.Trim();
mymoney.Money = (GetLastMoney() + Convert.ToDouble(MtextBox.Text)).ToString();
WriteMoney(mymoney.Money);
App.myDB.tables.InsertOnSubmit(mymoney);
App.myDB.SubmitChanges();
}
MessageBox.Show("操作成功");
MtextBox.Text = "";
StextBox.Text = "";
}
O(∩_∩)O~