Use Trace.Fail to alert a fail : Trace « Development Class « C# / C Sharp






Use Trace.Fail to alert a fail

  


using System;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
using System.Diagnostics;


class Class1 {
    [STAThread]
    static void Main(string[] args) {
        SqlConnection dbConn = new SqlConnection("server=.;database=pubs;uid=sa;pwd=");

        SqlCommand dbComm = new SqlCommand("SELECT * FROM " + "authors", dbConn);
        SqlDataReader dr = null;
        Trace.WriteLine(DateTime.Now + " - Executing SQL statement");
        try {
            dbConn.Open();
            Trace.Assert(dbConn.State == ConnectionState.Open,"Error", "Connection failed...");
            dr = dbComm.ExecuteReader(CommandBehavior.CloseConnection);
            Trace.Assert(dr != null, "Error","The SqlDataReader is null!");

            while (dr.Read()) {
            }
        } catch {
            Trace.Fail("An error occurred in database access");
        } finally {
            if ((dr.IsClosed == false) && (dr != null))
                dr.Close();
        }
    }
}

   
  








Related examples in the same category

1.Trace.WriteLine
2.demonstrates post-deployment tracingdemonstrates post-deployment tracing
3.demonstrates the use of TraceListener objectsdemonstrates the use of TraceListener objects
4.Design By Contract Checks.