Imports System
Imports System.Data
Imports System.Data.SqlTypes
Imports System.Data.SqlClient
Namespace DB
    Public Class clsMyKey_Tabelle
        Inherits db.clsKey_tabelle

        Public Function get_dbkey(ByVal Tablename As String) As Long
            Dim m_dbkey As Long
            Dim scmCmdToExecute As SqlCommand = New SqlCommand()
            scmCmdToExecute.CommandText = "dbo.[sp_get_dbkey]"
            scmCmdToExecute.CommandType = CommandType.StoredProcedure
            ' // Use base class' connection object
            scmCmdToExecute.Connection = m_scoMainConnection
            Try
                scmCmdToExecute.Parameters.Add(New SqlParameter("@Tablename", SqlDbType.VarChar, 255, ParameterDirection.Input, True, 0, 0, "", DataRowVersion.Proposed, Tablename))
                scmCmdToExecute.Parameters.Add(New SqlParameter("@dbkey", SqlDbType.Int, 4, ParameterDirection.Output, True, 10, 0, "", DataRowVersion.Proposed, m_dbkey))
                scmCmdToExecute.Parameters.Add(New SqlParameter("@iErrorCode", SqlDbType.Int, 4, ParameterDirection.Output, True, 10, 0, "", DataRowVersion.Proposed, m_iErrorCode))

                If m_bMainConnectionIsCreatedLocal Then
                    ' // Open connection.
                    m_scoMainConnection.Open()
                Else
                    If m_cpMainConnectionProvider.bIsTransactionPending Then
                        scmCmdToExecute.Transaction = m_cpMainConnectionProvider.stCurrentTransaction
                    End If
                End If

                ' // Execute query.
                Try
                    scmCmdToExecute.Connection.Open()
                Catch ex As Exception
                Finally
                End Try

                scmCmdToExecute.ExecuteNonQuery()
                m_dbkey = scmCmdToExecute.Parameters.Item("@dbkey").Value
                m_iErrorCode = New SqlInt32(CType(scmCmdToExecute.Parameters.Item("@iErrorCode").Value, SqlInt32))
                scmCmdToExecute.Connection.Close()

                If Not m_iErrorCode.Equals(New SqlInt32(LLBLError.AllOk)) Then
                    ' // Throw error.
                    Throw New Exception("Stored Procedure 'sp_get_dbkey' reported the ErrorCode: " & m_iErrorCode.ToString())
                End If

                Return m_dbkey
            Catch ex As Exception
                ' // some error occured. Bubble it to caller and encapsulate Exception object
                Throw New Exception("clsKey_tabelle::get_dbkey::Error occured." + ex.Message, ex)
            Finally
                If m_bMainConnectionIsCreatedLocal Then
                    ' // Close connection.
                    m_scoMainConnection.Close()
                End If
                scmCmdToExecute.Dispose()
            End Try
        End Function

    End Class
End Namespace
