Database Connection

  • Thread starter Thread starter MadCrazyNewbie
  • Start date Start date
M

MadCrazyNewbie

Hey group,

Sorry to bother you all again but ............ I did start this a while ago,
and Cor pointed me in the right direction but i`ve forgot as more important
things needed doing @ work.

So here Goes,

On my project I have a combobox on start up with:

Live - Network
Development - Network
Live - Local (Read-Only)

When I select one I want the appropriate Connection string to be passed to
my project.

Is this correct i say on my Login form have for example:

Dim ConnectNetworkLive As OLEDB.Connection
OLEDB.Connection = "\\Data01\Excellence.Net\NetworkLiveDB.MDB
Dim ConnectNetworkDeval As OLEDB.Connection
OLEDB.Connection = "\\Data01\Excellence.Net\NetworkDevDB.MDB
Dim ConnectLocal As OLEDB.Connection
OLEDB.Connection = "C:\Program Files\Ecellence.Net\LocalDB.MDB

Now I guess that when i select on from my ComboBox I need to use one of the
above Connection strings and Pass that to a value somewhere along the line?

My Questions: How would i pass the above to a string (If that right) and as
I have several Data Connectors on my Project - I guess i need to get rid of
them, but how would i tell my data Adapters to use the Connection string
selected, if you get what i mean?

Many Thanks
MCN
 
Hi Simon,

If I understand it well, do you have 3 networks however one application.

That application can use a connection to one of your 3 environments.

However for the program it is just a connection.

Because you have made yourr connection using the designer (if you did not
change that) is the only thing that you have to do setting the right
connection string to that connection you already have, before you open the
connection. (Using the OleDbDataAdapter.fill can do that automaticly for you
however you can do it also by code before that fill)
\\\
myConnection.connectionstring = "String from my combobox"
///
I hope this helps?

Cor
 
Cor,

How would i use my ComboBox, i've only really used it for binding to a DB, i
assume I would do something like to assign each line to a command?

How would i pass a string to my other forms from my ComboBox?

Many Thanks
MCN(Si)
 
Cor,

Many Thanks.for you reply.

So would i be correct in thinking i need to do:

Private Sub cboSelectDB_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cboSelectDB.SelectedIndexChanged

Dim ConLiveNet As new OLEDB.ConnectionString
ConLiveNet = "\\data01\Ambercat\Excellence.Net\LiveNetDB.MDB"
Dim ConDevNet As new OLEDB.ConnectionString
ConDevNet = "\\data01\Ambercat\Excellence.Net\DevNetDB.MDB"
Dim ConLiveLocal As new OLEDB.ConnectionString
ConLiveLocal = "C:\Program Files\Excellence.Net\LiveLocalDB.MDB"

cboConnection.items.add( "LiveNetDB")
cboConnection.selectedindex = 0
cboConnection.items.add( "LiveLocalDB")
cboConnection.selectedindex = 1 (Would this be right or would i just use 0
again?)
cboConnection.items.add( "DevNetDB")
cboConnection.selectedindex = 2 (Would this be right or would i just use 0
again?)

End Sub

Im still unsure or how to pass a string tho.

Also am i correct in thinking from what you said I need to delete my
DataConnectors and add the following code to each Dataform:

myConnection.connectionstring = "String from my combobox"

How ould i get it to determin which Database I selected from my ComboBox?

Sorry im just a little confused.

Many Thanks
MCN(Si)
 
Hi Simon,

I still do not see why you need all those connection strings I think that
something as this will do it, or I understand your application wrong.

Load event of the form1
dim frm2 as new form2
frm2.showdialog 'with that ok arround it
dim filelocation as string
dim myconnectionsstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
filelocation & ";"
myfilelocation = frm2.connectionstring
myOriginalconnection.connectionstring = myconnectionstring

And then in form2
In the load event
public connectionstring as string
cboConnection.items.add( "\\data01\Ambercat\Excellence.Net\LiveNetDB.MDB")
cboConnection.items.add( "C:\Program Files\Excellence.Net\LiveLocalDB.MDB")
cboConnection.items.add( "xxxxxx")
cboConnection.selectedinded = 1 ' than the combobox is in the middle.

And than in de combobobox selectedindex change event (do not use the
commited event that has a bug for this selections without a value)

connectionstring = cboConnection.text

It would be nicer to use an object than we could display the text and now
you see the places.

However that for the next time to improve.

All typed in this message, so watch typos or other errors.

Cor
 
Cor,

I`ve e-mailed you a copy of my App, I think it may enable you to understand
it better.

Cheers
Si
 
Hi Simon,

You changed such a lot I did not recognize it anymore.

You can make this class somewhere

Public Class connection
Private Shared mConnectionsstring As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
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("C:\Mad\bin\filename.mdb") 'You real 3 paths
'here the 2 others same way
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

Than in your login form after the Button OK click this
'because you have filled the values using the designer you can do this
connection.setString(cboSelectDB.SelectedIndex)

And in every form where you have connections direct in the begin of the Load
event.
odcPhotocopiersMeterReadings.ConnectionString = connection.String

' with the right connection of course

And do not forget to set the combobox to enabled = true .

I hope this goes?

Cor
 
What do you mean by the correct connection string? would this be this bit:

Public Class Connection?

Many Thanks
MCN
 
Hi Simon,

Not correct connection string, correct connection

In every form where you use a connection they have a different name one of
them is
odcPhotocopiersMeterReadings

I thought you all have named them odc..........

Cor
 
Ah right sorry Cor i get what you mean now, all my ODC`s have connection
strings in them, so do i just take that out in the designer now and use:

odcPhotocopiersMeterReadings.ConnectionString = connection.String

Or am i not understanding and been stupid?

Regards
MCN
 
Simon,

Keep them in the designer, that is one of the few parts which give can big
trouble when you take them out.

Just set the connection string in the load event to the right one what is
needed, using that code as bellow. Those few extra bytes would not be a
trouble in my opinion.

Cor
 
Back
Top