• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 939
  • Last Modified:

Override / Inherit sqlCommand Object

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.

1 Solution
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
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.

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now