今天完成了收藏功能。本来想用豆瓣账号登陆获取收藏列表的,不过后来一想我只要把歌曲信息记录在本地不是简单多了么。况且mango已经支持本地数据库,更能实践一下。
下面说说mango本地数据库开发。
这个其实如果你用过Linq to sql的话就很容易使用它了。
首先我们需要一个类来继承DbConent
using Model;
using System.Data.Linq;
namespace DbContent
{
public class DBFMDB :DataContext
{
public static string _connStr = "Data Source=isostore:/FavSong.sdf";
public DBFMDB(string connStr)
: base(connStr)
{ }
public Table<dbSong> favSong;
}
}
using System.Data.Linq;
namespace DbContent
{
public class DBFMDB :DataContext
{
public static string _connStr = "Data Source=isostore:/FavSong.sdf";
public DBFMDB(string connStr)
: base(connStr)
{ }
public Table<dbSong> favSong;
}
}
这里千万注意 public Table<dbSong> favSong;
你不能写成
public Table<dbSong> favSong{get;set;},不然报错!为什么?我也不知道。Model类:
using System.Data.Linq.Mapping;
namespace Model
{
[Table]
public class dbSong
{
private int _id;
[Column(IsPrimaryKey=true,
IsDbGenerated = true,
DbType = "INT NOT NULL Identity",
CanBeNull=false,
AutoSync=AutoSync.OnInsert)]
public int ID
{ get { return _id; } set { _id = value; } }
private string _url;
/// <summary>
/// MP3 URL
/// </summary>
[Column]
public string url
{
namespace Model
{
[Table]
public class dbSong
{
private int _id;
[Column(IsPrimaryKey=true,
IsDbGenerated = true,
DbType = "INT NOT NULL Identity",
CanBeNull=false,
AutoSync=AutoSync.OnInsert)]
public int ID
{ get { return _id; } set { _id = value; } }
private string _url;
/// <summary>
/// MP3 URL
/// </summary>
[Column]
public string url
{
get { returnthis._url; }
set { _url =value; }
}
。。。。
}
这里同上, 你不能写成 public string url {get;set;} 一定要像.NET 2.0的时候那样写属性。不然报错。为什么?我还是不知道。
最后是数据库操作类:
using System.Data.Linq;
using System.Linq;
using DbContent;
using Model;
using System.Collections.Generic;
namespace Helper
{
public class DbHelper
{
private static DBFMDB db;
/// <summary>
/// 创建
/// </summary>
public static void CreateDb()
{
if(db==null)
{
db = new DBFMDB(DBFMDB._connStr);
}
if (!db.DatabaseExists())
{
db.CreateDatabase();
}
}
public static void AddSong(dbSong song)
{
db.favSong.InsertOnSubmit(song);
db.SubmitChanges();
}
public static void DelSong(dbSong song)
{
dbSong oldS = db.favSong.Single(s => s.url == song.url);
db.favSong.DeleteOnSubmit(oldS);
db.SubmitChanges();
}
public static List<dbSong> GetFavSongList()
{
var list = db.favSong.ToList();
return list;
}
public static bool IsFavSong(dbSong song)
{
var obj = db.favSong.Where(o=>o.url==song.url);
if (obj != null&&obj.Count()>0)
return true;
else
return false;
}
}
using DbContent;
using Model;
using System.Collections.Generic;
namespace Helper
{
public class DbHelper
{
private static DBFMDB db;
/// <summary>
/// 创建
/// </summary>
public static void CreateDb()
{
if(db==null)
{
db = new DBFMDB(DBFMDB._connStr);
}
if (!db.DatabaseExists())
{
db.CreateDatabase();
}
}
public static void AddSong(dbSong song)
{
db.favSong.InsertOnSubmit(song);
db.SubmitChanges();
}
public static void DelSong(dbSong song)
{
dbSong oldS = db.favSong.Single(s => s.url == song.url);
db.favSong.DeleteOnSubmit(oldS);
db.SubmitChanges();
}
public static List<dbSong> GetFavSongList()
{
var list = db.favSong.ToList();
return list;
}
public static bool IsFavSong(dbSong song)
{
var obj = db.favSong.Where(o=>o.url==song.url);
if (obj != null&&obj.Count()>0)
return true;
else
return false;
}
}
}
这个就没什么好说的了~~
收工睡觉~~~