koschins Posted December 15, 2003 Posted December 15, 2003 Hi, I was trying to write a class to encapsulate the differences in the OleDb/SQLReader/ADO.NET data access methodes. The main problem is that ADO.NET does not support the same basic methods/properties as the OleDB/SQLReader classes. My approach was to implement a derived class of System.Data.DataTable and implement the missing core functions like HasRows() or Read(): Public Class DataTableRec Inherits System.Data.DataTable Protected recPos As Int64 = -1 Public Function HasRows() As Boolean If MyBase.Rows.Count > 0 Then Return True Else Return False End Function Public Function Read() As Boolean If recPos > MyBase.Rows.Count Then Return False recPos += 1 Return True End Function End Class The problem is that you can't assign a base class instance to a variable of the derived class type. Here a simple code bit to demonstrate the problem: Public Class _tBase Public bProp1 As String Public bProp2 As String End Class Public Class _tDerived Inherits _tBase Public cProp1 As String Public cProp2 As String End Class Sub _testInherit() Dim oBase As New _tBase Dim oDerived As New _tDerived ' This cast does not work (run-time error) oDerived = CType(oBase, _tDerived) End Sub Is that a general restriction of VB.NET Inheritance/type cast ? Or am I thinking in the wrong direction ? Problem is that you have an existing instance of an System.Data.DataTable but want to extend the interface. Any help is greatly appreciated ! Quote
Administrators PlausiblyDamp Posted December 15, 2003 Administrators Posted December 15, 2003 You can only cast a derived to a base not the other way round. In your example _tDerived IS a _tBase and therefore can be cast into a variable of that type. _tBase isn't however a _tDerived. Could you give a bit more detail on what you are trying to acheive? There may be an alternate way. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
koschins Posted December 16, 2003 Author Posted December 16, 2003 The goal is to provide a consistent interface for all database access methods - a library should wrap the calls of sqlclient/oledb/ado.net database connections methods, so that you can develop a function like Public Function db_recordset(sqlstring As String, ...) As Object ... If <adoconfig=ado.net> Then Dim dsAdapter As Object, dsDataSet As New System.Data.DataSet, recTable As New DataTableRec If <dbconfig=mssql> Then dsAdapter = New System.Data.sqlclient.SqlDataAdapter(sqlstring, CType(connObj, System.data.SqlClient.SqlConnection)) Else dsAdapter = New System.Data.OleDb.OleDbDataAdapter(sqlstring, CType(connObj, System.data.OleDb.OleDbConnection)) End If dsAdapter.Fill(dsDataSet, "sqltab") objRec = dsDataSet.Tables("sqltab") Else If <dbconfig=mssql> Then objCmd = New SqlClient.SqlCommand Else objCmd = New OleDb.OleDbCommand End If objCmd.CommandText = sqlstring objCmd.connection = connObj objRec = objCmd.ExecuteReader() End If db_recordset = objRec End Function A client using the function db_recordset() (latebinding through return type [object]) would have to have a consistent interface using methods/properties on the returned recordset object - primarily HasRows() and Read(). You could of course develop a whole new recordset class encapsulation all of the methods and properties (creating a "HAS A" relationship instead of "IS A" relationship) but then you'd have to reimplement all of the methods/properties and the client would loose the ability to use all of the methods/properties of the recordset through late binding (if it knows what type the db_recordset function is returning). What would be your approach trying to implement such a wrapper class ? Thanks for you reply ! Quote
Optikal Posted December 16, 2003 Posted December 16, 2003 Take a look at the data layer in MS's .Net Pet Store sample application (it can be found on MSDN). That handles abstracting away the data-access code. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.