Hi...
This is my [newbie] first attempt at creating a class to handle all my database connections, creating datasets and - eventully - inserts, updates and deletes.
I created this to avoid code duplication and needless call to and from the database, but I'm not sure that I've done anything better than what I started with (without the class). I still have a couple of subs to workout and complete, but I was hoping that those of you with the knowledge of this would take a look and let me know if I'm started down the right path here... I'd like to be as efficient as possible, but don't know if I'm accomplishing anything towards that goal...
What I have now (code below) is a form with a few tabs. Whe the form loads, a query is sent to the database for the different divisions and that is used as the titles on the respective tabs. It works now...
I'd really appreaciate any help or suggestions anyone could offer.
This is my [newbie] first attempt at creating a class to handle all my database connections, creating datasets and - eventully - inserts, updates and deletes.
I created this to avoid code duplication and needless call to and from the database, but I'm not sure that I've done anything better than what I started with (without the class). I still have a couple of subs to workout and complete, but I was hoping that those of you with the knowledge of this would take a look and let me know if I'm started down the right path here... I'd like to be as efficient as possible, but don't know if I'm accomplishing anything towards that goal...
What I have now (code below) is a form with a few tabs. Whe the form loads, a query is sent to the database for the different divisions and that is used as the titles on the respective tabs. It works now...
I'd really appreaciate any help or suggestions anyone could offer.
Code:
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ds As New DataSet
Dim sql As String
Dim db As New DBConn
sql = "SELECT DISTINCT Employees1.[Division] FROM Employees1 ORDER BY Employees1.[Division] DESC"
ds = db.get_data(sql)
Me.TabPage1.Text = ds.Tables("my_dataset").Rows(0).Item(0)
Me.TabPage2.Text = ds.Tables("my_dataset").Rows(1).Item(0)
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Dim con As OleDb.OleDbConnection
Dim db As DBConn
db.CloseDB(con)
End Sub
End Class
Public Class DBConn
Public Function get_data(ByVal sql As String) As DataSet
Dim con As New OleDb.OleDbConnection
con = OpenDB()
Dim da As OleDb.OleDbDataAdapter
If con.State = ConnectionState.Closed Then con.Open()
da = New OleDb.OleDbDataAdapter(sql, con)
Dim my_ds As New DataSet
da.Fill(my_ds, "my_dataset")
Return my_ds
End Function
Public Function CloseDB(ByRef db_con As OleDb.OleDbConnection)
db_con.Close()
[b][green]'*** not working -- need the current database connection to pass in???[/green][/b]
End Function
Public Function OpenDB() As OleDb.OleDbConnection
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = \\4fl-w2k01\public\OutOfOffice\OutOfOffice_Depot.mdb"
Return con
End Function
Public Function GetConn() As OleDb.OleDbConnection
Dim con As New OleDb.OleDbConnection
[b][green]'*** somehow get the current database connection and pass it back[/green][/b]
End Function
End Class