Database Connection

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I'm working with a Microsoft Access Database in VB.net. I can open a connection in my code as follows:

mConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & Data Source=" & dbString
dbConn = New System.data.OleDb.OleDbConnection(mConnection)

I'd like to put this code in its own class, and then instantiate it when needed.

In making this class I thought I'd need to inherit:
System.data.OleDb.OleDbConnection
But that doesn't appear to be inheritable.

Will this approach work? And, if so, how do I define this class?

I'd appreciate any suggestions.

Art
 
Hi Art,

How did you know that this thread went offline a much further and that I
wrote this for Simon.
\\\
Public Class Connection
Private Shared mLocation As String
Private Shared connArray As ArrayList
Public Shared Sub setString(ByVal ind As Integer)
Dim connArray As New ArrayList()
connArray.Add("location 1") 'the place of the file inclusief the
name of it
connArray.Add("location 2")
connArray.Add("location 3") ' as much as you want
mLocation = connArray(ind).ToString
End Sub
Public Shared ReadOnly Property [String]() As String
Get
Return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
mLocation & ";"
End Get
End Property
End Class
///
Simon had filed a combobox with 3 names, when the user choose than you can
do in the selected index change from the combobox

Connection.setString(combobox.selectedindex)

And when you need the string

myconn.connectionstring = Connection.String

Maybe you can use it too (it is not an efficient routine however it will be
used in a program only one time to set the index and maybe some times to get
the string)?

Cor
 
Art,

I do not know if this is a question,
however setting the connection string to the connection with
conn.connectionstring = myconnections
gives you the posibilitiy to reuse the connection again and again just by
opening it (after that it was closed or not yet opened of course)

I hope this was a/the question?

Cor
Thanks again for your help. Your description gives me a good way to
return the connection string. I wanted to know if I could return the
connection itself. I realize that just being able to have the connection
string available is very helpful, but I did want to know if I could do this
with the connection.
Even though I am actually trying to get some work done, I am also trying
to learn how best to use OOP -- something that is new to me.
Thanks again,

Art

Cor Ligthert said:
Hi Art,

How did you know that this thread went offline a much further and that I
wrote this for Simon.
\\\
Public Class Connection
Private Shared mLocation As String
Private Shared connArray As ArrayList
Public Shared Sub setString(ByVal ind As Integer)
Dim connArray As New ArrayList()
connArray.Add("location 1") 'the place of the file inclusief the
name of it
connArray.Add("location 2")
connArray.Add("location 3") ' as much as you want
mLocation = connArray(ind).ToString
End Sub
Public Shared ReadOnly Property [String]() As String
Get
Return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
mLocation & ";"
End Get
End Property
End Class
///
Simon had filed a combobox with 3 names, when the user choose than you can
do in the selected index change from the combobox

Connection.setString(combobox.selectedindex)

And when you need the string

myconn.connectionstring = Connection.String

Maybe you can use it too (it is not an efficient routine however it will be
used in a program only one time to set the index and maybe some times to get
the string)?

Cor
 
Back
Top