数据库表的-映射XML
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="MyCompany.MyProject.Db.PHS_LT4800_Main, MyCompany.MyProject" table="PHS_LT4800_Main">
<id name="ID" column="ID" type="Int64">
<generator class="identity" />
</id>
<property name="name" type="String(25)" column="name" />
<property name="num" type="String(10)" column="num" />
<property name="desce" type="String(100)" column="desce" />
<property name="parent" type="Int32" column="parent" />
</class>
</hibernate-mapping>
数据库表的-映射类
using System;
namespace MyCompany.MyProject.Db
{
public class PHS_LT4800_Main
{
public PHS_LT4800_Main()
{
}
private System.Int64 _ID;
public System.Int64 ID
{
get { return _ID; }
set { _ID = value; }
}
private System.String _num;
public System.String num
{
get { return _num; }
set { _num = value; }
}
private System.String _desce;
public System.String desce
{
get { return _desce; }
set { _desce = value; }
}
private System.String _name;
public System.String name
{
get { return _name; }
set { _name = value; }
}
private System.Int32 _parent;
public System.Int32 parent
{
get { return _parent; }
set { _parent = value; }
}
}
}
NH的配置文件-APP.CFG
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<nhibernate>
<add
key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"
/>
<add
key="hibernate.dialect"
value="NHibernate.Dialect.MsSql2000Dialect"
/>
<add
key="hibernate.connection.driver_class"
value="NHibernate.Driver.SqlClientDriver"
/>
<add
key="hibernate.connection.connection_string"
value="Server=localhost;initial catalog=phs;Integrated Security=SSPI;User id=sa;password=sfhlf;"
/>
</nhibernate>
<!-- This section contains the log4net configuration settings -->
<log4net>
<!-- Define some output appenders -->
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="log.txt" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy.MM.dd" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{ auth }> - %m%n" />
</layout>
</appender>
<!-- Setup the root category, add the appenders and set the default priority -->
<root>
<priority value="ALL" />
<appender-ref ref="rollingFile" />
</root>
</log4net>
</configuration>
实现代码:
Configuration cfg = new Configuration();
cfg.AddAssembly("MyCompany.MyProject");
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction transaction = session.BeginTransaction();
for(int i=0;i<1000;i++)
{
PHS_LT4800_Main pm=new PHS_LT4800_Main();
pm.name ="电话sdsdf"+i.ToString();
pm.num ="sdf"+i.ToString();
pm.parent =1;
session.Save(pm);
}
transaction.Commit();
session.Close();
好像不管单条还是多条,都是2~3秒左右