• Linq to mysql


    下载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;
                }
            }
  • 相关阅读:
    MiniUi绑定mini-combobox下拉框
    ORA-01658:无法为表空间XXX中的段创建initial区
    ora-01033:oracle initialization or shutdown in progress 解决方法
    MVC示例
    NHibernate概念
    流程的python PDF高清版
    正则指引 pdf 高清版
    第三方库:logger,自定义日志封装模块
    python2.* 版本 与 3.* 版本中的区别
    Selenium2+python自动化-文件上传
  • 原文地址:https://www.cnblogs.com/haorensw/p/2415690.html
Copyright © 2020-2023  润新知