Override / Inherit sqlCommand Object

Posted on 2011-05-02
Last Modified: 2012-05-11
Hi All,

I know its possible ot inherit / ovveride existing objects in VB.Net but its not somthing I've ever done.

I have the need to log SQL queries to a seperate database and rather than write a seperate sub then call it over and over again I was hoping that there might be a way to create a new object that inherits the sqlcommand object (in paticular the executescalar/nonquery). That way I can declare an instance of myOwnSQLcommand, and each time I call the ExecuteScalar/NonQuery functions not only would it perform the requested databse operation but it would also write the SQL query out to a seperate databse/sql server.

Is this possbile? I really looking for a step in the right direction here guys.

Question by:mgordon-spi
    LVL 16

    Expert Comment

    by:Imran Javed Zia
    You can do it as following:

    Create a base class say BaseDAL
    add required functions like ExecuteScalar/NonQuery and also any functionailty like CRUD features as per requirement. now your each and every custom object may inhert this and may call its methods to get work done.

    LVL 83

    Accepted Solution

    You can do it in two ways.

    1) Inheritance. But you would have to implement a lot of constructors etc for the base class.

    Public Class MySqlCommand
              Inherits SqlCommand
         Public Sub New()
         End Sub
         Public Sub New(sql As String, con As SqlConnection)
                MyBase.New(sql, con)
         End Sub
         Public Function ExecuteNonQuery() As Integer
              LogThisQuery(CommandText) 'This function saves the sql text to your other table.
              Return MyBase.ExecuteNonQuery(CommandText)
         End Function
    End Class

    The other method is to use functions where LogThisQuery function is called before executing the SqlCommand.

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now