菜鸟涛

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: :: 管理 ::

Let's start vb.net today!

今天要学习goal:就是vb.net怎么和数据库结合。怎么在vb.net用Nhibernate.

vb.net连接数据库流程:

1.通过一些操作数据库的dll如:ADO.NET和ODBC.NET ,来创建一个connection。

2.建立connection成功,进行CRUD来操作Database. 这里有些概念:DataSet和DataTable

3.销毁connection(Request)。

相当于MS官方提供的数据库Client DLL ,Nhibernate更能使开发员脱离一些数据转换和一些sql语句的编写,更面向对象,使开发人员更专注于业务。呵呵,自己用了几年滴hibernate,对其还算了解。

 Let's image a story first:

1. 用户登录时,这时会去在Database(test)中表t_user里去查询。

2.t_user里的column: id ,name,password,email。。

ok,Let's do it。

1,我们先建个database,名字为test,在建个表t_user. 这里我使用database是mysql,呵呵。

2,导入Nhibernate的dll。去官网http://nhforge.org/下载最新版NHibernate,解压缩到硬盘,在导入NHibernate.dll:visual studio 2010 里点project 右键--》Add Reference ,选择NHibernate.dll路径导入。如图:

 

 下载Mysql的.net framework data provider. http://dev.mysql.com/downloads/connector/net/

 3.需要建立几个文件夹(如上图),entity和mapping。  entity--》class。 mapping:是xml文件,用来描述entity里的class属性和数据库表字段的对应关系。

未完,lunch 。。。

4.新建一个hiberna.cfg.xml 连接数据库的配置文件。这里可以导入NHibernate的xsd模板到visual studio2010里,具体做法:

   1)在hiberna.cfg.xml点属性, Define "Embedded Resource" as Build Action for this xml file. 如图


 2) 在设置Schemas,导入NHibernate里的nhibernate-configuration.xsd

 

 Ok,配置文件:

<?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='proxyfactory.factory_class'>NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
    <property name="connection.connection_string">Server=192.168.1.20;Port=3309;Database=test;User ID=xxx;Password=xxxx;</property>
    
<property name="show_sql">true</property>
  
</session-factory>

</hibernate-configuration> 

 5.在entity文件夹里定义一个User类,Properties: id,userName,password,email.定义get,set方法。代码片段:

Public Class User

    
Private id As Integer
    
Private userName As String
    
Private password As String
    
Private email As String

    
Public Sub New()
    
End Sub

    
Public Property Id() As String
        
Get
            
Return id
        
End Get
        
Set(ByVal Value As String)
            id 
= Value
        
End Set
    
End Property

End Class 

新语法 Property :用于标识一个类成员为属性而不是方法.属性可以被获取,属性在被获以的时候利用get句柄返回其值.属性可以被赋值,这个过程是由set句柄完成的.这两个句柄可以不同时存在.如果只有get而没有set,那属性就是只读的.如果只有set而没有get,那属性就是只写的.

6.编写.hbm.xml文件,代码片段:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" >

  
<!-- more mapping info here -->
  
<class name="User,helloworld" table="t_user" lazy="false">
    
<id name="id"  type="integer" column="id">
      
<generator class="native" />
    
</id>
    
<property name="userName" column ="name" type="string"/>
    
<property name="password" column ="password" type="string"/>
    
<property name="email" column="email" type="string"/>
  
</class>

</hibernate-mapping> 

7.编写Nhibernate连接数据库代码。 代码片段:

 Dim myConfig As New Configuration
 myConfig.Configure(
"hibernate.cfg.xml")
 myConfig.SetProperty(
"hibernate.dialect",   "NHibernate.Dialect.MySQL5Dialect")
 myConfig.AddAssembly(
"helloworld")
 
Dim myFactory As ISessionFactory = myConfig.BuildSessionFactory
 
Dim mySession As ISession = myFactory.OpenSession
 
Dim myTransaction As ITransaction = mySession.BeginTransaction
  
Dim _user As New User
        _user.userName 
= "jack"
        _user.password 
= "xxx1"
        _user.email 
= "xxxx"


  mySession.Save(_user)
  myTransaction.Commit()

  mySession.Close()

注意:

1.这里还要导入一些dll,完整dll如图:

 

 2.hibernate.cfg.xml 和 xxx.hbm.xml 要设置为Define "Embedded Resource" as Build Action for this xml file。这样这些xml文件就会自动导入到运行工程文件夹里去。

 

 Persist

 

 

 

posted on 2011-04-19 11:28  菜鸟涛  阅读(297)  评论(2编辑  收藏  举报