[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 907
  • 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 ZiaCommented:
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.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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