Database Connection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MadCrazyNewbie

    Database Connection

    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 ConnectNetworkL ive As OLEDB.Connectio n
    OLEDB.Connectio n = "\\Data01\Excel lence.Net\Netwo rkLiveDB.MDB
    Dim ConnectNetworkD eval As OLEDB.Connectio n
    OLEDB.Connectio n = "\\Data01\Excel lence.Net\Netwo rkDevDB.MDB
    Dim ConnectLocal As OLEDB.Connectio n
    OLEDB.Connectio n = "C:\Program Files\Ecellence .Net\LocalDB.MD B

    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



  • scorpion53061

    #2
    Re: Database Connection

    > On my project I have a combobox on start up with:[color=blue]
    >
    > Live - Network
    > Development - Network
    > Live - Local (Read-Only)
    >
    > When I select one I want the appropriate Connection string to be passed to
    > my project.[/color]

    You could use an xml file that gives you the option of where your conneciton
    string is located...



    If you need help with this concept let me know.


    Comment

    • Cor Ligthert

      #3
      Re: Database Connection

      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 OleDbDataAdapte r.fill can do that automaticly for you
      however you can do it also by code before that fill)
      \\\
      myConnection.co nnectionstring = "String from my combobox"
      ///
      I hope this helps?

      Cor


      Comment

      • MadCrazyNewbie

        #4
        Re: Database Connection

        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 Ligthert" <notfirstname@p lanet.nl> wrote in message
        news:edt7Oj$REH A.620@TK2MSFTNG P10.phx.gbl...[color=blue]
        > 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 OleDbDataAdapte r.fill can do that automaticly for[/color]
        you[color=blue]
        > however you can do it also by code before that fill)
        > \\\
        > myConnection.co nnectionstring = "String from my combobox"
        > ///
        > I hope this helps?
        >
        > Cor
        >
        >[/color]


        Comment

        • Cor Ligthert

          #5
          Re: Database Connection

          Hi Simon,

          You mean
          combobox1.items .add( "ConnectionStri ng1")
          combobox1...... ......etc
          combobox1.selec tedindex =0



          And use the combobox.text or the selecteditem what you want

          I hope this was your question?

          Cor


          Comment

          • MadCrazyNewbie

            #6
            Re: Database Connection

            Cor,

            Many Thanks.for you reply.

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

            Private Sub cboSelectDB_Sel ectedIndexChang ed(ByVal sender As System.Object,
            ByVal e As System.EventArg s) Handles cboSelectDB.Sel ectedIndexChang ed

            Dim ConLiveNet As new OLEDB.Connectio nString
            ConLiveNet = "\\data01\Amber cat\Excellence. Net\LiveNetDB.M DB"
            Dim ConDevNet As new OLEDB.Connectio nString
            ConDevNet = "\\data01\Amber cat\Excellence. Net\DevNetDB.MD B"
            Dim ConLiveLocal As new OLEDB.Connectio nString
            ConLiveLocal = "C:\Program Files\Excellenc e.Net\LiveLocal DB.MDB"

            cboConnection.i tems.add( "LiveNetDB" )
            cboConnection.s electedindex = 0
            cboConnection.i tems.add( "LiveLocalD B")
            cboConnection.s electedindex = 1 (Would this be right or would i just use 0
            again?)
            cboConnection.i tems.add( "DevNetDB")
            cboConnection.s electedindex = 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.co nnectionstring = "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)

            "Cor Ligthert" <notfirstname@p lanet.nl> wrote in message
            news:etTwoTISEH A.3124@TK2MSFTN GP12.phx.gbl...[color=blue]
            > Hi Simon,
            >
            > You mean
            > combobox1.items .add( "ConnectionStri ng1")
            > combobox1...... ......etc
            > combobox1.selec tedindex =0
            >
            >[/color]
            http://msdn.microsoft.com/library/de...itemstopic.asp[color=blue]
            >
            > And use the combobox.text or the selecteditem what you want
            >
            > I hope this was your question?
            >
            > Cor
            >
            >[/color]


            Comment

            • Cor Ligthert

              #7
              Re: Database Connection

              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 myconnectionsst ring="Provider= Microsoft.Jet.O LEDB.4.0;Data Source=" &
              filelocation & ";"
              myfilelocation = frm2.connection string
              myOriginalconne ction.connectio nstring = myconnectionstr ing

              And then in form2
              In the load event
              public connectionstrin g as string
              cboConnection.i tems.add( "\\data01\Amber cat\Excellence. Net\LiveNetDB.M DB")
              cboConnection.i tems.add( "C:\Program Files\Excellenc e.Net\LiveLocal DB.MDB")
              cboConnection.i tems.add( "xxxxxx")
              cboConnection.s electedinded = 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)

              connectionstrin g = cboConnection.t ext

              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



              Comment

              • MadCrazyNewbie

                #8
                Re: Database Connection

                Cor,

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

                Cheers
                Si

                "Cor Ligthert" <notfirstname@p lanet.nl> wrote in message
                news:uSM2mqISEH A.1160@TK2MSFTN GP09.phx.gbl...[color=blue]
                > 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 myconnectionsst ring="Provider= Microsoft.Jet.O LEDB.4.0;Data Source=" &
                > filelocation & ";"
                > myfilelocation = frm2.connection string
                > myOriginalconne ction.connectio nstring = myconnectionstr ing
                >
                > And then in form2
                > In the load event
                > public connectionstrin g as string
                > cboConnection.i tems.add( "\\data01\Amber cat\Excellence. Net\LiveNetDB.M DB")
                > cboConnection.i tems.add( "C:\Program[/color]
                Files\Excellenc e.Net\LiveLocal DB.MDB")[color=blue]
                > cboConnection.i tems.add( "xxxxxx")
                > cboConnection.s electedinded = 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)
                >
                > connectionstrin g = cboConnection.t ext
                >
                > 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
                >
                >
                >[/color]


                Comment

                • Cor Ligthert

                  #9
                  Re: Database Connection

                  Hi Simon,

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

                  You can make this class somewhere

                  Public Class connection
                  Private Shared mConnectionsstr ing As String = _
                  "Provider=Micro soft.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\file name.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=Micro soft.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.setS tring(cboSelect DB.SelectedInde x)

                  And in every form where you have connections direct in the begin of the Load
                  event.
                  odcPhotocopiers MeterReadings.C onnectionString = connection.Stri ng

                  ' with the right connection of course

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

                  I hope this goes?

                  Cor


                  Comment

                  • MadCrazyNewbie

                    #10
                    Re: Database Connection

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

                    Public Class Connection?

                    Many Thanks
                    MCN
                    [color=blue][color=green][color=darkred]
                    >>>>>SNIP[/color][/color]
                    >
                    > And in every form where you have connections direct in the begin of the[/color]
                    Load[color=blue]
                    > event.
                    > odcPhotocopiers MeterReadings.C onnectionString = connection.Stri ng
                    >
                    > ' with the right connection of course
                    >
                    > And do not forget to set the combobox to enabled = true .
                    >
                    > I hope this goes?
                    >
                    > Cor
                    >
                    >[/color]


                    Comment

                    • Cor Ligthert

                      #11
                      Re: Database Connection

                      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
                      odcPhotocopiers MeterReadings

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

                      Cor
                      [color=blue]
                      > What do you mean by the correct connection string? would this be this bit:[/color]


                      Comment

                      • MadCrazyNewbie

                        #12
                        Re: Database Connection

                        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:

                        odcPhotocopiers MeterReadings.C onnectionString = connection.Stri ng

                        Or am i not understanding and been stupid?

                        Regards
                        MCN

                        "Cor Ligthert" <notfirstname@p lanet.nl> wrote in message
                        news:eRHn17KSEH A.1388@TK2MSFTN GP09.phx.gbl...[color=blue]
                        > 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
                        > odcPhotocopiers MeterReadings
                        >
                        > I thought you all have named them odc..........
                        >
                        > Cor
                        >[color=green]
                        > > What do you mean by the correct connection string? would this be this[/color][/color]
                        bit:[color=blue]
                        >
                        >[/color]


                        Comment

                        • Cor Ligthert

                          #13
                          Re: Database Connection

                          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
                          [color=blue]
                          > 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:
                          >
                          > odcPhotocopiers MeterReadings.C onnectionString = connection.Stri ng[/color]


                          Comment

                          • Cor Ligthert

                            #14
                            Re: Database Connection

                            sh.....[color=blue]
                            >
                            > Keep them in the designer, that is one of the few parts which give can big
                            > trouble when you take them out.[/color]
                            can give


                            Comment

                            Working...