下载Dblinq-0.20.1
编写批处理文件,放在Dblinq文件夹中,生成数据操作类。
@echo off DbMetal.exe -provider="MySql" -conn="Data Source=localhost;port=3306;User Id=***;Password=***" -database="***" -namespace="***" -code="mysql\***.cs" -sprocs
把生成的***.cs文件复制到项目中
修改生成的代码:更改继承到DbLinq.MySql.MySqlDataContext。同时编写数据连接返回方法。更改第一个数据连接,同时删除其它错误的方法。
public partial class **** : DbLinq.MySql.MySqlDataContext { #region Extensibility Method Declarations partial void OnCreated(); #endregion private static MySql.Data.MySqlClient.MySqlConnection MyCon { get { string constr = ConfigurationManager.ConnectionStrings["***"].ConnectionString; return new MySql.Data.MySqlClient.MySqlConnection(constr); } } public **() : base(MyCon) { this.OnCreated(); }
编写操作文件:
public class User { /// <summary> /// 获取整个列表 /// </summary> /// <returns></returns> static public IList<Entity.User> Get() { Entity.*** cl = new Entity.***(); IList<Entity.User> users = (from u in cl.User select u).ToArray(); cl.Dispose(); return users; } /// <summary> /// 查询单个列表 /// </summary> /// <param name="username"></param> /// <returns></returns> static public Entity.User Get(string username) { Entity.*** cl = new Entity.***(); Entity.User user = cl.User.SingleOrDefault(u => u.UserName == username); cl.Dispose(); return user; } /// <summary> /// 插入用户 /// </summary> /// <param name="user"></param> static public void Insert(Entity.User user) { Entity.*** cl = new Entity.***(); cl.User.InsertOnSubmit(user); try { cl.SubmitChanges(); } catch (Exception e) { throw e; } finally { cl.Dispose(); } } /// <summary> /// 删除用户 /// </summary> /// <param name="user"></param> static public void Delete(Entity.User user) { Entity.*** cl = new Entity.***(); Entity.User _user = cl.User.SingleOrDefault(u => u.UserName == user.UserName); if (_user == null) { cl.Dispose(); throw new Exception(""); } cl.User.DeleteOnSubmit(_user); cl.SubmitChanges(); cl.Dispose(); } /// <summary> /// 更新用户 /// </summary> /// <param name="user"></param> /// <returns></returns> static public bool Update(Entity.User user) { Entity.*** cl = new Entity.***(); user = cl.User.SingleOrDefault(u => u.UserName == user.UserName); if (user == null) { return false; } cl.SubmitChanges(); cl.Dispose(); return true; } /// <summary> /// 用户登录 /// </summary> /// <param name="username"></param> /// <returns></returns> static public bool Login(Entity.User user) { Entity.*** cl = new Entity.***(); Entity.User _user = cl.User.SingleOrDefault(u => u.UserName == user.UserName); if (user == null) { return false; } else { if (user.Password == _user.Password) return true; else return false; } }