Click here to Skip to main content
15,891,136 members
Articles / Database Development / SQL Server

How to Integrate an OLE Object with SQL Server v2

Rate me:
Please Sign up or sign in to vote.
4.97/5 (20 votes)
22 Aug 2009CPOL5 min read 39.1K   229   24  
This article will give you an idea about how to integrate an OLE Object that is a COM+ API with SQL Server and write a message into a Windows event log.
using System;
using System.Diagnostics;
using System.Text;

namespace OLEExample
{
   public class ClsExample
    {
        private bool IsExist(string strLogName)
        {
            bool Reasult = false;

            if (System.Diagnostics.EventLog.SourceExists(strLogName)) return Reasult = true;
            return Reasult;

        }
        public bool CreateLog(string strLogName)
        {
            bool Reasult = false;

            try
            {
                if (!this.IsExist(strLogName))
                {
                    System.Diagnostics.EventLog.CreateEventSource(strLogName, strLogName);
                    System.Diagnostics.EventLog SQLEventLog = new System.Diagnostics.EventLog();

                    SQLEventLog.Source = strLogName;
                    SQLEventLog.Log = strLogName;

                    SQLEventLog.Source = strLogName;
                    SQLEventLog.WriteEntry("The " + strLogName + " was successfully initialize component.", EventLogEntryType.Information);


                    Reasult = true;

                }
            }
            catch
            { 
                Reasult = false; 
            }


            return Reasult;
        }

        #region "Windows event log writer"

            public void Write_ERROR_EventLog(string strLogName
                                           , string strSource
                                           , string strErrDetail)
            {
                if (this.IsExist(strLogName))
                {

                    System.Diagnostics.EventLog SQLEventLog = new System.Diagnostics.EventLog();


                    try
                    {


                        SQLEventLog.Source = strLogName;
                        SQLEventLog.WriteEntry(Convert.ToString(strSource)
                                              + Convert.ToString(strErrDetail), EventLogEntryType.Error);

                    }
                    catch (Exception ex)
                    {
                        SQLEventLog.Source = strLogName;
                        SQLEventLog.WriteEntry(Convert.ToString("ERROR: ")
                                              + Convert.ToString(ex.Message), EventLogEntryType.Error);


                    }
                    finally
                    {
                        SQLEventLog.Dispose();
                        SQLEventLog = null;


                    }
                }
            }

            public void Write_WARNING_EventLog(string strLogName
                                             , string strSource
                                             , string strErrDetail)
            {
                if (this.IsExist(strLogName))
                {

                    System.Diagnostics.EventLog SQLEventLog = new System.Diagnostics.EventLog();

                    try
                    {


                        SQLEventLog.Source = strLogName;
                        SQLEventLog.WriteEntry(Convert.ToString(strSource)
                                              + Convert.ToString(strErrDetail), EventLogEntryType.Warning);

                    }
                    catch (Exception ex)
                    {
                        SQLEventLog.Source = strLogName;
                        SQLEventLog.WriteEntry(Convert.ToString("WARNING: ")
                                              + Convert.ToString(ex.Message), EventLogEntryType.Warning);


                    }
                    finally
                    {
                        SQLEventLog.Dispose();
                        SQLEventLog = null;


                    }
                }
            }


            public void Write_INFO_EventLog(string strLogName
                                          , string strSource
                                          , string strErrDetail)
            {
                if (this.IsExist(strLogName))
                {

                    System.Diagnostics.EventLog SQLEventLog = new System.Diagnostics.EventLog();

                    try
                    {

                        SQLEventLog.Source = strLogName;
                        SQLEventLog.WriteEntry(Convert.ToString(strSource)
                                              + Convert.ToString(strErrDetail), EventLogEntryType.Information);

                    }
                    catch (Exception ex)
                    {
                        SQLEventLog.Source = strLogName;
                        SQLEventLog.WriteEntry(Convert.ToString("INFORMATION: ")
                                              + Convert.ToString(ex.Message), EventLogEntryType.Information);


                    }
                    finally
                    {
                        SQLEventLog.Dispose();
                        SQLEventLog = null;

                    }
                }
            }

            public void Write_FAILUR_EventLog(string strLogName
                                            , string strSource
                                            , string strErrDetail)
            {
                if (this.IsExist(strLogName))
                {

                    System.Diagnostics.EventLog SQLEventLog = new System.Diagnostics.EventLog();

                    try
                    {

                        SQLEventLog.Source = strLogName;
                        SQLEventLog.WriteEntry(Convert.ToString(strSource)
                                              + Convert.ToString(strErrDetail), EventLogEntryType.FailureAudit );

                    }
                    catch (Exception ex)
                    {
                        SQLEventLog.Source = strLogName;
                        SQLEventLog.WriteEntry(Convert.ToString("FAILUR: ")
                                              + Convert.ToString(ex.Message), EventLogEntryType.FailureAudit );


                    }
                    finally
                    {

                        SQLEventLog.Dispose();
                        SQLEventLog = null;


                    }
                }
            }

            public void Write_SUCCESS_EventLog(string strLogName
                                            , string strSource
                                            , string strErrDetail)
            {
                if (this.IsExist(strLogName))
                {

                    System.Diagnostics.EventLog SQLEventLog = new System.Diagnostics.EventLog();

                    try
                    {

                        SQLEventLog.Source = strLogName;
                        SQLEventLog.WriteEntry(Convert.ToString(strSource)
                                              + Convert.ToString(strErrDetail), EventLogEntryType.SuccessAudit );

                    }
                    catch (Exception ex)
                    {
                        SQLEventLog.Source = strLogName;
                        SQLEventLog.WriteEntry(Convert.ToString("FAILUR: ")
                                              + Convert.ToString(ex.Message), EventLogEntryType.SuccessAudit );


                    }
                    finally
                    {

                        SQLEventLog.Dispose();
                        SQLEventLog = null;


                    }
                }
            }


        #endregion

            public void Write_AUTHOR_EventLog(string strLogName
                                          , string strSource
                                          , string strErrDetail)
            {
                if (this.IsExist(strLogName))
                {

                    System.Diagnostics.EventLog SQLEventLog = new System.Diagnostics.EventLog();

                    try
                    {

                        SQLEventLog.Source = strLogName;
                        SQLEventLog.WriteEntry(Convert.ToString(strSource)
                                              + Convert.ToString(strErrDetail), EventLogEntryType.Information);

                    }
                    catch (Exception ex)
                    {
                        SQLEventLog.Source = strLogName;
                        SQLEventLog.WriteEntry(Convert.ToString("INFORMATION: ")
                                              + Convert.ToString(ex.Message), EventLogEntryType.Information);


                    }
                    finally
                    {
                        SQLEventLog.Dispose();
                        SQLEventLog = null;

                    }
                }
            }

            public string GetAuthor()
            { return "Md. Marufuzzaman"; }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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



Comments and Discussions