C#中通过映射对MySQL数据进行操作

                                首先,C#中通过映射对MySQL数据进行操作需要导入:FluentNHibernate.dll和NHibernate.dll这两个程序集;引用方法:
C#中通过映射对MySQL数据进行操作_第1张图片
选择联机搜索,然后点击安装就可以了;
右键添加新项目,添加一个NhibernateHelper类用来打开MySQL会话,代码如下;
    class NhibernateHelper
    {
        private static ISessionFactory sessionFactory = null;
        private static void InitSessionFactory()
        {
		//FluentConfiguration fConfig = Fluently.Configure();
            //MySQLConfiguration mySQLConfig=MySQLConfiguration.Standard.ConnectionString(db => db.Server("localhost").Database("taidou").Username("root").Password("root"));
            //sessionFactory=fConfig.Database(mySQLConfig).Mappings(x => x.FluentMappings.AddFromAssemblyOf()).BuildSessionFactory();
            sessionFactory = Fluently.Configure().Database(MySQLConfiguration.Standar   d.ConnectionString(db=>db.Server("localhost").Database("taidou").Username("root").Password("root"))).Mappings(x=>x.FluentMappings.AddFromAssemblyOf()).BuildSessionFactory();//连接MySQL数据库
        }
        private static ISessionFactory SessionFactory {
            get {
                if (sessionFactory == null)
                    InitSessionFactory();
                return sessionFactory;
            }
        }
        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();   //打开Session会话
        }
    }
然后再添加一个对数据库数据进行操作的类: TestUserManager
    class TestUserManager
    {
        public IList GetAllUser()    //获得用户表中的所有数据
        {
            using (var session = NhibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var userList = session.QueryOver();
                    transaction.Commit();   //提交操作
                    return userList.List();
                }
            }
        }
        public IList GetUserByUsername(string username)    //根据username查找数据
        {
            using (var session = NhibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var userList = session.QueryOver().Where(user => user.Username == username);
                    transaction.Commit();
                    return userList.List();
                }
            }
        }
        public void SaveUser(TestUser user)   //添加user并保存到数据库,相当于添加数据;
        {
            using (var session = NhibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Save(user);
                    transaction.Commit();
                }
            }
        }
        public void UpdateUser(TestUser user)   //更新修改后的数据
        {
            using (var session = NhibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Update(user);
                    transaction.Commit();
                }
            }
        } 
        public void DeleteById(int id)     //根据ID删除数据;
        {
            using (var session = NhibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    TestUser tu = new TestUser();
                    tu.Id = id;
                    session.Delete(tu);
                    transaction.Commit();
                }
            }
        }
    }
添加一个类,类名为TestUser,其中的方法要与数据库表中字段一致,方便映射:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyServer.Model
{
    class TestUser
    {
        public virtual int Id { get; set; }
        public virtual string Username { get; set; }
        public virtual string Password { get; set; }
        public virtual int Age { get; set; }
    }
}
C#中通过映射对MySQL数据进行操作_第2张图片
再添加一个映射类TestUserMap,需要用到命名空间:using FluentNHibernate.Mapping;
using FluentNHibernate.Mapping;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyServer.Model.Mapping
{
    class TestUserMap:ClassMap
    {
        public TestUserMap()//这里的x时textuser对象;
        {
            Id(x => x.Id).Column("Id");//设置Id属性为主键,column为匹配数据库字段
            Map(x => x.Username).Column("username");
            Map(x => x.Password).Column("password");
            Map(x => x.Age).Column("age");
            Table("testuser");
        }
    }
}
好了,暂时就这么多方法,后续再更新,用的时候来学习一下就不会忘记啦!!!




你可能感兴趣的:(C#编程)