Sunday, February 5, 2012

try catch & DB Connection

SqlConnection conn = null;
SqlCommand cmd = null;

try
{
    conn = new SqlConnection(Settings.Default.qlsdat_extensionsConnectionString)
    cmd = new SqlCommand(reportDataSource, conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@Year", SqlDbType.Char, 4).Value = year;
    cmd.Parameters.Add("@startDate", SqlDbType.DateTime).Value = start;
    cmd.Parameters.Add("@endDate", SqlDbType.DateTime).Value = end;
        cmd.Open();

    DataSet dset = new DataSet();
    new SqlDataAdapter(cmd).Fill(dset);
    this.gridDataSource.DataSource = dset.Tables[0];
}
catch(Exception ex)
{
    Logger.Log(ex);
    throw;
}
finally
{
    if(conn != null)
        conn.Dispose();

        if(cmd != null)
        cmd.Dispose();
}

*****************
 Try        
Catch ex As Exception            
'Exception handling here        
Finally 'Clean Up
If Conn.State = ConnectionState.Open Then Conn.Close()            
If Conn IsNot Nothing Then Conn.Dispose()            
If cmd IsNot Nothing Then cmd.Dispose()
  'AndAlso only tests 2nd condition if 1st is 'True'            
If myReader IsNot Nothing AndAlso Not (myReader.IsClosed) Then myReader.Close()        
End Try

************
Public Function ExecuteNonQuery(ByVal cmd As String, ByVal cmdType As CommandType, Optional ByVal parameters() As SqlParameter = Nothing) As Integer
            Dim connection As SqlConnection = Nothing
            Dim transaction As SqlTransaction = Nothing
            Dim command As SqlCommand = Nothing
            Dim res As Integer = -1
            Try
                connection = New SqlConnection(_connectionString)
                command = New SqlCommand(cmd, connection)
                command.CommandType = cmdType
                Me.AssignParameters(command, parameters)
                connection.Open()
                transaction = connection.BeginTransaction()
                command.Transaction = transaction
                res = command.ExecuteNonQuery()
                transaction.Commit()
            Catch ex As Exception
                If Not (transaction Is Nothing) Then
                    transaction.Rollback()
                End If
                Throw New SqlDatabaseException(ex.Message, ex.InnerException)
            Finally
                If Not (connection Is Nothing) AndAlso (connection.State = ConnectionState.Open) Then connection.Close()
                If Not (command Is Nothing) Then command.Dispose()
                If Not (transaction Is Nothing) Then transaction.Dispose()
            End Try
            Return res
        End Function


************
 Finally
                'Lily on Feb 2012
                If Not (reader.IsClosed) Then
                    reader.Close()
                End If
                If reader IsNot Nothing Then
                    reader = Nothing
                End If
                If Not (ppResult Is Nothing) Then
                    ppResult = Nothing
                End If
                If Not (pp Is Nothing) Then
                    pp.Dispose()
                    pp = Nothing
                End If
                If cmd IsNot Nothing Then
                    cmd.Dispose()
                End If
                cmd = Nothing
                If conn.State = ConnectionState.Open Then
                    conn.Close()
                End If
                If conn IsNot Nothing Then
                    conn.Dispose()
                End If
                conn = Nothing
                If flagAR = False Then
                    Threading.Thread.CurrentThread.Sleep(timeReTry)
                End If
            End Try

No comments:

Post a Comment