PhotonServer游戏服务器学习利用NHibernate和MySQL进行交互(上)

一、在程序集中找到管理NuGet程序包,安装MySQL.Data和NHibernate,要求版本不要过高,有可能安装不成功

 二、创建两个文件夹,分别存放两个不同的xml格式的文件

注意:第一个文件下的xml文件命名需要“表名.hbm.xml”;第二个文件一定写成“hibernate.cfg.xml”。

 

1、firstone类里面存放成员变量及其属性 (用来对应数据库表格里面的数据,多个数据就需要多个变量,成员变量要求是virtual类型的)

namespace 利用NHibernate和MySQL进行交互.Model
{
    internal class firstone
    {
        public virtual int ID { get; set; }
        public virtual string Name { get; set; }
        public virtual int Age { get; set; }
        public virtual DateTime Time { get; set; }
    }

2、firstone.hbm.xml文件

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
				   assembly="利用NHibernate和MySQL进行交互"
				   namespace="利用NHibernate和MySQL进行交互.Model">
	<class name="firstone" table="firstone">
		<id name="ID" column="ID" type="Int32">
			<generator class="native"></generator>
		</id>
		<property name="Name" column="Name" type="String"></property>
		<property name="Age" column="Age" type="Int32"></property>
		<property name="Time" column="Time" type="Date"></property>
	</class>
</hibernate-mapping>

3、hibernate.cfg.xml文件

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
	<session-factory>
		<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
		<property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
		<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
		<!--使用什么数据库-->
		<!--<property name="connection.connection_string">Server=localhost;Database=20203322刘磊;User ID=root;Password=liulei5412647;</property>-->
		<property name="connection.connection_string">Server=localhost;Database=20203322刘磊;Port=3305;User ID=root;Password=liulei5412647;</property>
		<property name="show_sql">true</property>
	</session-factory>
</hibernate-configuration>

三、在program类里进行数据库的一系列操作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NHibernate;
using NHibernate.Cfg;
using 利用NHibernate和MySQL进行交互.Model;

namespace 利用NHibernate和MySQL进行交互
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var configration = new Configuration();
            configration.Configure("hibernate.cfg.xml");//解析nhibernate.cfg.xml
            configration.AddAssembly("利用NHibernate和MySQL进行交互");//解析 映射文件 firstine.hbm.xml...
            ISessionFactory sessionFactory = null;
            ISession session = null;
            try
            {
                sessionFactory = configration.BuildSessionFactory();
                session = sessionFactory.OpenSession();//打开一个跟数据库的会话
                firstone firstone = new firstone() { Name = "小舞", Age = 18 };
                session.Save(firstone);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                if (session != null)
                {
                    session.Close();
                }
                if (sessionFactory != null)
                {
                    sessionFactory.Close();
                }
            }
            Console.ReadKey();
        }
    }
}

注意:Unable to connect to any of the specified MySQL hosts报错原因:数据库的地址没有写对 或者没打开MySQL

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值