Classi

martedì 25 luglio 2006 - 19.50

massivabene Profilo | Newbie

Ciao a tutti.

Ho una classe completa di accesso al database ed altre classi che definiscono ognuna la struttura delle tabelle del database. All'interno di quelle classe ho dei metodi che istanziano la prima classe.
Secondo voi, dal punto di vista delle performance e della programmazione è meglio lasciare le cose così oppure è meglio ereditare la classe di accesso al database?
Grazie

19018 Profilo | Expert

Se non vuoi ereditare credo che ti convenga dichiarare i metodi per l'accesso al db tutti statici. Questa tecnica viene utilizzata quando una classe offre dei "servizi" che vengono richiesti da più oggetti. In questo modo hai una unica istanza in memoria dei metodi utilizzati e non devi allocare tante volte sempre lo stesso codice.
ciao

Stefano Passatordi

http://blogs.dotnethell.it/stem/

massivabene Profilo | Newbie

Ti ringrazio per la risposta, ma non ho capito......

Brainkiller Profilo | Guru

Dichiarando i metodi come :

public static

poi possono essere richiamati senza istanziare la classe esempio:

public abstract class SQL { public static void AddRank(DataSet ds) { return; } }

una volta fatta questa il metodo AddRank lo puoi chiamare semplicemente così:

SQL.AddRank(ds);

senza dover istanziare nessuna classe.
E' uno dei metodi + usati, poi dipende... il tuo progetto mi sembra decisamente + complesso.Le classi d'accesso le hai scritte tu ?
ciao


David De Giacomi
Microsoft MVP
http://blogs.dotnethell.it/david/

massivabene Profilo | Newbie

si, se vuoi posso anche postarle in modo che eventualmente anche altri le possano sfruttare e nello stesso tempo magari qualcuno dà un giudizio.

massivabene Profilo | Newbie

Questa è la classe di connessione.

Imports IBM.Data.DB2
Imports System.Data.OleDb
Public Class Ado
Private Const Db2linConnString As String = "database=xxxx;UID=xxxx;PWD=xxxx"

Private Const SqldsConnString As String
= "database=SQLDS;UID=yyyy;PWD=yyyy;Connect Timeout=0"

Private Const DbprsConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xxxxx;"

Private Const CommissConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xxxxxx;"

Private Const ProvaConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xxxxx;"

Dim cnString As String
Dim cnDb2 As DB2Connection
Dim cnOledb As OleDbConnection
Dim WithEvents DaDb2 As DB2DataAdapter
Dim WithEvents DaOledb As OleDbDataAdapter
Public Dt As DataTable
Public Ds As DataSet
Public DrDb2 As DB2DataReader
Public DrOledb As OleDbDataReader
Public Row As DataRow
Dim Nr As Integer
Dim provider As Integer
Sub New()
provider = 0
Nr = 0
cnString = ""
End Sub
Private Function PutDatasouce(ByVal datasource As String) As Boolean

Dim IsDatasource As Boolean = True

If datasource.Length = 0 Then
Throw New Exception("Manca nome origine dati")
End If

Dim myDatasource As String
myDatasource = datasource.ToUpper

Select Case myDatasource
Case "DB2LIN"
cnString = Db2linConnString
provider = 0
Case "SQLDS"
cnString = SqldsConnString
provider = 0
Case "DBPRS"
cnString = DbprsConnString
provider = 1
Case "COMMISS"
cnString = CommissConnString
provider = 1
Case "PROVA"
cnString = ProvaConnString
provider = 1
Case Else
IsDatasource = False
End Select

Return IsDatasource
End Function
Private Sub Open()
If provider = 0 Then
cnDb2 = New DB2Connection(cnString)
cnDb2.Open()
Else
cnOledb = New OleDbConnection(cnString)
cnOledb.Open()
End If
End Sub
Private Sub Dispose()
If provider = 0 Then
DaDb2.Dispose()
Else
DaOledb.Dispose()
End If
End Sub
Public Function GetData(ByVal datasource As String, ByVal sqlstring As String, ByVal table As String) As Integer

If PutDatasouce(datasource) = False Then
Throw New Exception("GetData " & datasource & " Nome origine dati errata")
End If
If sqlstring.Length = 0 Then
Throw New Exception("GetData " & "Manca dichiarazione Sql")
End If

Try
Open()
Dim IsConnecting As Boolean = True
While IsConnecting
If provider = 0 Then
DaDb2 = New DB2DataAdapter(sqlstring, cnString)
Else
DaOledb = New OleDbDataAdapter(sqlstring, cnString)
End If
Ds = New DataSet
If provider = 0 Then
Nr = DaDb2.Fill(Ds, table)
Else
Nr = DaOledb.Fill(Ds, table)
End If
Dt = Ds.Tables(table)
IsConnecting = False
End While

Return Nr
Catch e As Exception
Throw e
Finally
Close()
Dispose()
End Try
End Function
Public Function ExecuteCommand(ByVal datasource As String, ByVal sqlstring As String) As Integer

If PutDatasouce(datasource) = False Then
Throw New Exception("ExecuteCommand " & datasource & " Nome origine dati errata")
End If
If sqlstring.Length = 0 Then
Throw New Exception("ExecuteCommand " & "Manca dichiarazione Sql")
End If

Try
Open()
If provider = 0 Then
Dim cmd As New DB2Command(sqlstring, cnDb2)
Nr = cmd.ExecuteNonQuery()
Else
Dim cmd As New OleDbCommand(sqlstring, cnOledb)
Nr = cmd.ExecuteNonQuery()
End If
Return Nr
Catch e As Exception
Throw e
Finally
Close()
End Try
End Function
Public Sub ExecuteSingleRow(ByVal datasource As String, ByVal sqlstring As String)

If PutDatasouce(datasource) = False Then
Throw New Exception("ExecuteSingleRow " & datasource & " Nome origine dati errata")
End If
If sqlstring.Length = 0 Then
Throw New Exception("ExecuteSingleRow " & "Manca dichiarazione Sql")
End If

Try
Open()
If provider = 0 Then
Dim cmd As New DB2Command(sqlstring, cnDb2)
DrDb2 = cmd.ExecuteReader(CommandBehavior.SingleRow)
Else
Dim cmd As New OleDbCommand(sqlstring, cnOledb)
DrOledb = cmd.ExecuteReader(CommandBehavior.SingleRow)
End If
Catch e As Exception
CloseReader()
Close()
Throw e
End Try
End Sub
Public Function OpenReader(ByVal datasource As String, ByVal sqlstring As String) As Integer

If PutDatasouce(datasource) = False Then
Throw New Exception("OpenReader " & datasource & " Nome origine dati errata")
End If
If sqlstring.Length = 0 Then
Throw New Exception("OpenReader " & "Manca dichiarazione Sql")
End If

Try
Open()
If provider = 0 Then
Dim cmd As New DB2Command(sqlstring, cnDb2)
DrDb2 = cmd.ExecuteReader
Else
Dim cmd As New OleDbCommand(sqlstring, cnOledb)
DrOledb = cmd.ExecuteReader
End If
Catch e As Exception
CloseReader()
Close()
Throw e
End Try
End Function
Public Sub CloseReader()
If provider = 0 Then
If Not DrDb2 Is Nothing Then
If DrDb2.IsClosed = False Then
DrDb2.Close()
End If
End If
Else
If Not DrOledb Is Nothing Then
If DrOledb.IsClosed = False Then
DrOledb.Close()
End If
End If
End If
End Sub
Public Sub Close()
If provider = 0 Then
If Not cnDb2 Is Nothing Then
If cnDb2.State = ConnectionState.Open Then
cnDb2.Close()
End If
End If
Else
If Not cnOledb Is Nothing Then
If cnOledb.State = ConnectionState.Open Then
cnOledb.Close()
End If
End If
End If
End Sub
End Class

Brainkiller Profilo | Guru

>Questa è la classe di connessione.

Ok, mettila magari fra due tag [code ] e [/code ] senza spazio in modo che il post viene + corto e il listato è visibile in una pagina apposta.

Ti facevo sta domanda perchè l'Enterprise Library di Mircosoft contiene un Layer DAL apposito solo che non c'è DB2, c'è però l'OleDB. Però vedo che tu qui usi proprio le classi di IBM quindi bisognerebbe reintegrare la Enterprise Library. A sto punto va bene ciò che hai fatto.

David De Giacomi
Microsoft MVP
http://blogs.dotnethell.it/david/

massivabene Profilo | Newbie

[code ]
***** Con questa classe è possibile collegarsi ad un DB DB2 oppure ad un DB Access.
***** Volendo si possono aggiungere altri Db apportando delle semplici modifiche.
***** Se avete problemi nell'utilizzo chiedetemi pure.

Imports IBM.Data.DB2
Imports System.Data.OleDb
Public Class Ado
Private Const Db2linConnString As String = "database=Db2Lin;UID=xxxx;PWD=xxxx"
Private Const SqldsConnString As String = "database=SQLDS;UID=lanfprod;PWD=xxxx;Connect Timeout=0"
Private Const DbprsConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\xxxx\xxxx.mdb;"
Private Const CommissConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\xxxx\xxxxx.mdb;"
Private Const ProvaConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\xxxx\xxxxx\xxxxx.mdb;"
Dim cnString As String
Dim cnDb2 As DB2Connection
Dim cnOledb As OleDbConnection
Dim WithEvents DaDb2 As DB2DataAdapter
Dim WithEvents DaOledb As OleDbDataAdapter
Public Dt As DataTable
Public Ds As DataSet
Public DrDb2 As DB2DataReader
Public DrOledb As OleDbDataReader
Public Row As DataRow
Dim Nr As Integer
Dim provider As Integer
Sub New()
provider = 0
Nr = 0
cnString = ""
End Sub
Private Function PutDatasouce(ByVal datasource As String) As Boolean

Dim IsDatasource As Boolean = True

If datasource.Length = 0 Then
Throw New Exception("Manca nome origine dati")
End If

Dim myDatasource As String
myDatasource = datasource.ToUpper

Select Case myDatasource
Case "DB2LIN"
cnString = Db2linConnString
provider = 0
Case "SQLDS"
cnString = SqldsConnString
provider = 0
Case "DBPRS"
cnString = DbprsConnString
provider = 1
Case "COMMISS"
cnString = CommissConnString
provider = 1
Case "PROVA"
cnString = ProvaConnString
provider = 1
Case Else
IsDatasource = False
End Select

Return IsDatasource
End Function
Private Sub Open()
If provider = 0 Then
cnDb2 = New DB2Connection(cnString)
cnDb2.Open()
Else
cnOledb = New OleDbConnection(cnString)
cnOledb.Open()
End If
End Sub
Private Sub Dispose()
If provider = 0 Then
DaDb2.Dispose()
Else
DaOledb.Dispose()
End If
End Sub
Public Function GetData(ByVal datasource As String, ByVal sqlstring As String, ByVal table As String) As Integer

If PutDatasouce(datasource) = False Then
Throw New Exception("GetData " & datasource & " Nome origine dati errata")
End If
If sqlstring.Length = 0 Then
Throw New Exception("GetData " & "Manca dichiarazione Sql")
End If

Try
Open()
Dim IsConnecting As Boolean = True
While IsConnecting
If provider = 0 Then
DaDb2 = New DB2DataAdapter(sqlstring, cnString)
Else
DaOledb = New OleDbDataAdapter(sqlstring, cnString)
End If
Ds = New DataSet
If provider = 0 Then
Nr = DaDb2.Fill(Ds, table)
Else
Nr = DaOledb.Fill(Ds, table)
End If
Dt = Ds.Tables(table)
IsConnecting = False
End While

Return Nr
Catch e As Exception
Throw e
Finally
Close()
Dispose()
End Try
End Function
Public Function ExecuteCommand(ByVal datasource As String, ByVal sqlstring As String) As Integer

If PutDatasouce(datasource) = False Then
Throw New Exception("ExecuteCommand " & datasource & " Nome origine dati errata")
End If
If sqlstring.Length = 0 Then
Throw New Exception("ExecuteCommand " & "Manca dichiarazione Sql")
End If

Try
Open()
If provider = 0 Then
Dim cmd As New DB2Command(sqlstring, cnDb2)
Nr = cmd.ExecuteNonQuery()
Else
Dim cmd As New OleDbCommand(sqlstring, cnOledb)
Nr = cmd.ExecuteNonQuery()
End If
Return Nr
Catch e As Exception
Throw e
Finally
Close()
End Try
End Function
Public Sub ExecuteSingleRow(ByVal datasource As String, ByVal sqlstring As String)

If PutDatasouce(datasource) = False Then
Throw New Exception("ExecuteSingleRow " & datasource & " Nome origine dati errata")
End If
If sqlstring.Length = 0 Then
Throw New Exception("ExecuteSingleRow " & "Manca dichiarazione Sql")
End If

Try
Open()
If provider = 0 Then
Dim cmd As New DB2Command(sqlstring, cnDb2)
DrDb2 = cmd.ExecuteReader(CommandBehavior.SingleRow)
Else
Dim cmd As New OleDbCommand(sqlstring, cnOledb)
DrOledb = cmd.ExecuteReader(CommandBehavior.SingleRow)
End If
Catch e As Exception
CloseReader()
Close()
Throw e
End Try
End Sub
Public Function OpenReader(ByVal datasource As String, ByVal sqlstring As String) As Integer

If PutDatasouce(datasource) = False Then
Throw New Exception("OpenReader " & datasource & " Nome origine dati errata")
End If
If sqlstring.Length = 0 Then
Throw New Exception("OpenReader " & "Manca dichiarazione Sql")
End If

Try
Open()
If provider = 0 Then
Dim cmd As New DB2Command(sqlstring, cnDb2)
DrDb2 = cmd.ExecuteReader
Else
Dim cmd As New OleDbCommand(sqlstring, cnOledb)
DrOledb = cmd.ExecuteReader
End If
Catch e As Exception
CloseReader()
Close()
Throw e
End Try
End Function
Public Sub CloseReader()
If provider = 0 Then
If Not DrDb2 Is Nothing Then
If DrDb2.IsClosed = False Then
DrDb2.Close()
End If
End If
Else
If Not DrOledb Is Nothing Then
If DrOledb.IsClosed = False Then
DrOledb.Close()
End If
End If
End If
End Sub
Public Sub Close()
If provider = 0 Then
If Not cnDb2 Is Nothing Then
If cnDb2.State = ConnectionState.Open Then
cnDb2.Close()
End If
End If
Else
If Not cnOledb Is Nothing Then
If cnOledb.State = ConnectionState.Open Then
cnOledb.Close()
End If
End If
End If
End Sub
End Class
[/code]

Brainkiller Profilo | Guru

Fra [code ] e [/code ]

Non fra <code> e </code>

Togli lo spazio prima della parentesi chiusa se no mi renderizza codice [code ] e [/code ]

Puoi anche non creare un nuovo post ma modificare ciò che hai scritto con l'apposito link modifica messaggio.
ciao


David De Giacomi
Microsoft MVP
http://blogs.dotnethell.it/david/
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5