Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Option Strict On
Imports System.Data.OleDb
Imports System.IO

Imports System.Data.SqlClient

Public Class DataAccess
    'For Local DB
    Protected SQL_CONNECTION_STRING As String
    Dim cnSQL As SqlConnection
    Dim trSQL As SqlTransaction

    Protected DidPreviouslyConnect As Boolean = False
    Public Event ConnectionStatusChange(ByVal status As String)
    Public Event ConnectionFailure(ByVal reason As String)
    Public Event ConnectionCompleted(ByVal success As Boolean)

    Public Function CreateSQLDataSet(ByVal strSQL As String) As DataSet
        Dim RetDS As New DataSet
        Dim GeneralDA As SqlDataAdapter
        ' Raise a status event saying that the user is attempting to connect.
        ' This only needs to be done the very first time a connection is
        ' attempted.  After we've determined that MSDE or SQL Server is
        ' installed, this message no longer needs to be displayed.
        If Not DidPreviouslyConnect Then
            RaiseEvent ConnectionStatusChange("Connecting to SQL Server")
        End If

        Dim IsConnecting As Boolean = True
        While IsConnecting
            Try
                ' The SqlConnection class allows you to communicate with SQL Server.
                ' The constructor accepts a connection string as an argument.  This
                ' connection string uses Integrated Security, which means that you 
                ' must have a login in SQL Server, or be part of the Administrators
                ' group for this to work.
                'cnSQL As New SqlConnection(SQL_CONNECTION_STRING)
                ' A SqlCommand object is used to execute the SQL commands.
                Dim scmd As New SqlCommand(strSQL, cnSQL)
                ' A SqlDataAdapter uses the SqlCommand object to fill a DataSet.
                GeneralDA = New SqlDataAdapter(scmd)
                ' A SqlCommandBuilder automatically generates the SQL commands needed
                ' to update the database later.
                Dim scb As New SqlCommandBuilder(GeneralDA)
                ' Create a new DataSet and fill its first DataTable.
                RetDS = New DataSet()
                GeneralDA.Fill(RetDS)


                IsConnecting = False
                DidPreviouslyConnect = True
            Catch exp As Exception
                ' Couldn't connect to SQL Server.  Now try MSDE.
                'RaiseEvent ConnectionFailure("Unable To Connect SQL Server")
                Throw New ApplicationException("Unable To Connect SQL Server")

            End Try
        End While
        RaiseEvent ConnectionCompleted(True)
        Return RetDS
    End Function
    Public Function SQLDBTransaction(ByVal strQry As String) As Integer
        
        Dim l_intRowsEffected As Integer

        Dim cmSQL As SqlCommand

        If Not DidPreviouslyConnect Then
            RaiseEvent ConnectionStatusChange("Connecting to SQL Server")
        End If
        Dim IsConnecting As Boolean = True
        Try

            cmSQL = New SqlCommand(strQry, cnSQL, trSQL)
            cmSQL.CommandType = CommandType.Text
            'l_intRowsEffected = cmSQL.ExecuteNonQuery
            l_intRowsEffected = CInt(cmSQL.ExecuteScalar())
            If l_intRowsEffected = 0 Then
                File.AppendAllText("DBErrorLog.dat", Now.ToString & " Insert Fiil " & strQry & vbNewLine)
            End If
            ' Close and Clean up objects
            cmSQL.Dispose()
            cmSQL = Nothing
            SQLDBTransaction = l_intRowsEffected
        Catch ex As Exception
            File.AppendAllText("DBErrorLog.dat", Now.ToString & " - " & ex.Message & " -" & strQry & vbNewLine)
            'trSQL.Rollback()
            SQLDBTransaction = 0
            Throw New ApplicationException(ex.Message)
        End Try
        RaiseEvent ConnectionCompleted(True)
        Return SQLDBTransaction
    End Function
  

    Public Sub New(ByVal ApplicationName As String)
        Try
            '
            
            'SQL_CONNECTION_STRING = "Data Source=testserver\sql2k8test;Initial Catalog=HRMS;User ID=abc;Password=abc;application name=HRMS;packet size=4096;persist security info=True;"

            'SQL_CONNECTION_STRING = System.IO.File.ReadAllText("Connect.dat")
            cnSQL = New SqlConnection(SQL_CONNECTION_STRING)
            cnSQL.Open()
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
        

    End Sub
    Public Sub Close()
        cnSQL.Close()
        cnSQL.Dispose()
        cnSQL = Nothing
        If trSQL IsNot Nothing Then
            trSQL.Dispose()
            trSQL = Nothing
        End If
        
    End Sub
    Public Sub BeginTrans()
        trSQL = cnSQL.BeginTransaction
    End Sub
    Public Sub CommitTrans()
        If trSQL IsNot Nothing Then
            trSQL.Commit()
        End If
    End Sub
    Public Sub RollBackTrans()
        If trSQL IsNot Nothing Then
            trSQL.Rollback()
        End If

    End Sub
End Class
Posted
Updated 26-Mar-12 23:19pm
v3
Comments
faizan912 27-Mar-12 2:40am    
i m on assignment to understand this code somebody plz help me
and give explanation of this code
JF2015 27-Mar-12 2:57am    
Edited to add code formatting.
krumia 27-Mar-12 3:01am    
What is the problem? Which part do you not understand? To me it looks fairly commented. And meaningful identifiers used. Don't panic. Sit back and read. You will understand it surely.

1 solution

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
VB
Dim next as Integer = r.Next()

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment (unlike your example), line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900