VB.NET とオラクルの接続(com+ コンポーネント)

'これは自分で作ったCOM+コンポーネントです。EnterpriseServicesのContextUtiを利用して、二つの更新操作は
'一緒に実行します。

Imports System.EnterpriseServices
Imports System.Data.SqlClient
Imports Oracle.DataAccess.Client
Imports System.Data.Common

<Assembly: ApplicationName("WorxTransactions")>
<Assembly: ApplicationAccessControl(True)>
<Transaction(TransactionOption.RequiresNew)> _
Public Class BankTransactions
    Inherits ServicedComponent

    Public Sub transferMoney(ByVal amount As Decimal)
        Dim connstr As String = "User Id=appus; Password=app;Data Source=(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = bacdb)(PORT = 1521)))(CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = TOCERA2)))"
        Dim connstr2 As String = "User Id=system; Password=manager;Data Source=(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = DELL-716)(PORT = 1521)))(CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = edfr)))"
        Try
            'お金足りるか足らないかチェック
            Withdraw(connstr, amount)
            Try
                '二つのACCOUNTの更新操作
                Deposit(connstr, connstr2, amount)
            Catch ex As Exception
                'deposit failed
                Throw New ApplicationException("Error transfering money,deposit failed.", ex)
            End Try
          Catch ex As Exception
            'withdraw failed
            Throw New ApplicationException("Error transfering money,withdrawl failed.", ex)

        End Try
    End Sub

    Private Sub Withdraw(ByVal connectionString As String, ByVal TranAmount As Decimal)

        Dim currentValue As String
        Dim daBack As DataSet = New DataSet
        Dim conn As New OracleConnection
        Dim oraAdapter As OracleDataAdapter = Nothing
        Dim oraCommand As OracleCommand = conn.CreateCommand()
        conn.ConnectionString = connectionString
        oraCommand.Connection = DirectCast(conn, OracleConnection)

        Try
            conn.Open()
            oraCommand.CommandText = "SELECT AMOUNT FROM ACCOUNTS WHERE NAME='He'"
            oraAdapter = New OracleDataAdapter(oraCommand)
            oraAdapter.Fill(daBack)
            currentValue = daBack.Tables(0).Rows(0).Item(0).ToString

            If currentValue < TranAmount Then
                Throw New ArgumentException("Attempt to overdraft account")
            End If

        Catch ex As Exception
            Throw New DataException("Error withdrawing", ex)
        Finally
            If Not conn Is Nothing Then
                conn.Close()
            End If
        End Try

    End Sub

    Private Sub Deposit(ByVal connectionString As String, ByVal connectionString2 As String, ByVal TranAmount As Decimal)
        Dim conn As New OracleConnection(connectionString)
        Dim conn2 As New OracleConnection(connectionString2)
        Dim command As OracleCommand
        Dim command2 As OracleCommand

        conn.Open()
        command = New OracleCommand("", conn)

        conn2.Open()
        command2 = New OracleCommand("", conn2)

        Try
            command.CommandText = "UPDATE ACCOUNTS SET AMOUNT = AMOUNT - " & TranAmount & " WHERE NAME ='He'"
            command.ExecuteNonQuery()

            command2.CommandText = "UPDATE ACCOUNTS SET AMOUNT =" & TranAmount & " + AMOUNT WHERE NAME ='Me'"
            command2.ExecuteNonQuery()

            '二つの更新の操作は一緒にコミットする
            ContextUtil.SetComplete()

        Catch ex As Exception
            ContextUtil.SetAbort()
            Throw New DataException("Error withdrawing", ex)
        Finally
            conn.Close()
            conn2.Close()
        End Try
    End Sub

End Class
===============下の部分はFORM1のコードです==================================

Imports Transactions

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim txn As New BankTransactions
        Try
            txn.transferMoney(CDec(Me.TransferField.Text))

            MessageBox.Show(String.Format("{0:C} transfered from {1} to {2}", _
            CDec(Me.TransferField.Text), "He", "Me"), _
            "Transfer Succeeded", MessageBoxButtons.OK, MessageBoxIcon.Information)

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Transfer Failed", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try
    End Sub

End Class

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值