Nhibernate (1) 操作数据库 增删改

第一次接触确实门槛很高,多亏yok大哥的细心指导,dnc的群给我带来这么多.net的好朋友,真开心!
java can!i can!-->Nhibernate
这是第一个数据库增删改的小例子,就当是出丑了。

Imports System.Collections
Imports NHibernate
Imports NHibernate.Cfg
Namespace pb.testdal.Db


Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写 dispose 以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents Button1 As System.Windows.Forms.Button
        Friend WithEvents Button2 As System.Windows.Forms.Button
        Friend WithEvents Button3 As System.Windows.Forms.Button
        Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.Button1 = New System.Windows.Forms.Button
            Me.Button2 = New System.Windows.Forms.Button
            Me.Button3 = New System.Windows.Forms.Button
            Me.DataGrid1 = New System.Windows.Forms.DataGrid
            CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.SuspendLayout()
            '
            'Button1
            '
            Me.Button1.Location = New System.Drawing.Point(40, 32)
            Me.Button1.Name = "Button1"
            Me.Button1.Size = New System.Drawing.Size(88, 23)
            Me.Button1.TabIndex = 0
            Me.Button1.Text = "添加一行数据"
            '
            'Button2
            '
            Me.Button2.Location = New System.Drawing.Point(40, 72)
            Me.Button2.Name = "Button2"
            Me.Button2.Size = New System.Drawing.Size(88, 23)
            Me.Button2.TabIndex = 1
            Me.Button2.Text = "更新一行数据"
            '
            'Button3
            '
            Me.Button3.Location = New System.Drawing.Point(40, 112)
            Me.Button3.Name = "Button3"
            Me.Button3.Size = New System.Drawing.Size(88, 23)
            Me.Button3.TabIndex = 2
            Me.Button3.Text = "删除一行数据"
            '
            'DataGrid1
            '
            Me.DataGrid1.DataMember = ""
            Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
            Me.DataGrid1.Location = New System.Drawing.Point(152, 32)
            Me.DataGrid1.Name = "DataGrid1"
            Me.DataGrid1.Size = New System.Drawing.Size(288, 152)
            Me.DataGrid1.TabIndex = 3
            '
            'Form1
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
            Me.ClientSize = New System.Drawing.Size(456, 222)
            Me.Controls.Add(Me.DataGrid1)
            Me.Controls.Add(Me.Button3)
            Me.Controls.Add(Me.Button2)
            Me.Controls.Add(Me.Button1)
            Me.Name = "Form1"
            Me.Text = "Form1"
            CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
            Me.ResumeLayout(False)

        End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim cfg As New Configuration
            cfg.AddAssembly("WindowsApplication1")
            cfg.AddXmlFile("../qqq.hbm.xml")
            Dim factory As ISessionFactory = cfg.BuildSessionFactory()
            Dim session As ISession = factory.OpenSession()
            Dim trx As ITransaction = session.BeginTransaction()

            Dim vList As IList = session.Find("from WindowsApplication1.pb.testdal.Db.qqq")

            DataGrid1.DataSource = vList
            DataGrid1.SetDataBinding(vList, "")

            trx.Commit()
            session.Close()

        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            '添加数据
            Dim cfg As New Configuration
            cfg.AddAssembly("WindowsApplication1")
            cfg.AddXmlFile("../qqq.hbm.xml")
            Dim factory As ISessionFactory = cfg.BuildSessionFactory()
            Dim session As ISession = factory.OpenSession()
            Dim trx As ITransaction = session.BeginTransaction()

            Dim q As New qqq

            q.ID = "7"
            q.aaa = "a"
            q.bbb = "b"

            session.Save(q)

            trx.Commit()
            session.Close()

 

        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            '更新数据
            Dim cfg As New Configuration
            cfg.AddAssembly("WindowsApplication1")
            cfg.AddXmlFile("../qqq.hbm.xml")
            Dim factory As ISessionFactory = cfg.BuildSessionFactory()
            Dim session As ISession = factory.OpenSession()
            Dim trx As ITransaction = session.BeginTransaction()

            Dim q As New qqq

            q.ID = "1"
            q.aaa = "aa"
            q.bbb = "bb"

            session.Update(q, "1")

            trx.Commit()
            session.Close()

        End Sub

        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            '删除数据
            Dim cfg As New Configuration
            cfg.AddAssembly("WindowsApplication1")
            cfg.AddXmlFile("../qqq.hbm.xml")
            Dim factory As ISessionFactory = cfg.BuildSessionFactory()
            Dim session As ISession = factory.OpenSession()
            Dim trx As ITransaction = session.BeginTransaction()

            Dim q As New qqq

            q.ID = "2"

            session.Delete(q)

            trx.Commit()
            session.Close()

        End Sub
    End Class
End Namespace

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值