欢迎光临

木子屋

标题: C#用文本文件做数据库示例

作者: dnawo

日期: 2014-08-22 00:20 AM

分类: Win编程

内容: 1.数据集基类
复制内容到剪贴板 程序代码/// <summary>
/// 数据集基类
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class BaseTB<T>
where T : class
{
private static string _TextFile = Path.Combine(ConfigurationManager.AppSettings["TextDBDir"], typeof(T).Name + "TB.txt");
private static List<T> _data = null;
/// <summary>
/// 数据容器
/// </summary>
internal static List<T> Container
{
get
{
if (_data == null)
{
if (File.Exists(_TextFile))
{
_data = JsonHelper.Deserialize<List<T>>(File.ReadAllText(_TextFile));
}
else
{
_data = new List<T>();
}
}
return _data;
}
}
/// <summary>
/// 获取记录
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public abstract T Get(T obj);
/// <summary>
/// 属性复制
/// </summary>
/// <param name="obj1"></param>
/// <param name="obj2"></param>
public abstract void Copy(T obj1, T obj2);
/// <summary>
/// 添加记录
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public bool Add(T obj)
{
bool result = false;
T item = Get(obj);
if (item == null)
{
Container.Add(obj);
Save();
result = true;
}
return result;
}
/// <summary>
/// 更新记录
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public bool Update(T obj)
{
bool result = false;
T item = Get(obj);
if (item != null)
{
Copy(item, obj);
Save();
result = true;
}
return result;
}
/// <summary>
/// 删除记录
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public bool Remove(T obj)
{
bool result = false;
T item = Get(obj);
if (item != null)
{
Container.Remove(item);
Save();
result = true;
}
return result;
}
/// <summary>
/// 保存数据
/// </summary>
internal void Save()
{
File.WriteAllText(_TextFile, JsonHelper.Serialize(Container));
}
}
2.实体及数据集类
复制内容到剪贴板 程序代码/// <summary>
/// 实体类
/// </summary>
public class User
{
public string Usn { get; set; }
public string Pwd { get; set; }
}
/// <summary>
/// 数据集
/// </summary>
public class UserTB : BaseTB<User>
{
#region 实现父类方法
/// <summary>
/// 获取对象
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public override User Get(User user)
{
return Get(user.Usn);
}
/// <summary>
/// 属性复制
/// </summary>
/// <param name="user1"></param>
/// <param name="user2"></param>
public override void Copy(User user1, User user2)
{
user1.Usn = user2.Usn;
user1.Pwd = user2.Pwd;
}
#endregion
/// <summary>
/// 获取对象
/// </summary>
/// <param name="usn"></param>
/// <returns></returns>
public User Get(string usn)
{
User result = null;
foreach (User item in Container)
{
if (item.Usn.ToLower() == usn.ToLower())
{
result = item;
break;
}
}
return result;
}
/// <summary>
/// 删除记录
/// </summary>
/// <param name="usn"></param>
public void Remove(string usn)
{
User user = new User() { Usn = usn };
Remove(user);
}
}
3.数据库类
复制内容到剪贴板 程序代码/// <summary>
/// 数据库
/// </summary>
public static class Database
{
/// <summary>
/// 初始化
/// </summary>
static Database()
{
if (!Directory.Exists(ConfigurationManager.AppSettings["TextDBDir"]))
{
Directory.CreateDirectory(ConfigurationManager.AppSettings["TextDBDir"]);
}
}
private static UserTB _usertb;
/// <summary>
/// UserTB
/// </summary>
public static UserTB UserTB
{
get
{
if (_usertb == null)
{
_usertb = new UserTB();
}
return _usertb;
}
}
}
调用示例
复制内容到剪贴板 程序代码User user = new User() { Usn = "dnawo", Pwd = "666666" };
Database.UserTB.Add(user);
user.Pwd = "999999";
Database.UserTB.Update(user);

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.196 ms

返回

标题: C#用文本文件做数据库示例

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.196 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 1.244 ms

返回