Open one connect and use in different form

xswzaq

Well-known member
Joined
Jul 9, 2007
Messages
61
Programming Experience
Beginner
Is there a way to open only one connection and use it in many different form? Like I have this code in FORM1

Dim conn As New OracleConnection("Data Source=xxxx;User Id=xxxx;Password=xxxx;Integrated Security=no;")

Try

conn.Open()

If conn.State = ConnectionState.Open Then



Dim Part_Adapter As New OracleClient.OracleDataAdapter("SELECT PART FROM PART GROUP BY PART ORDER BY PART", conn)

Dim Part_Dataset As New DataSet

Part_Adapter.Fill(Part_Dataset)

Dim dt As New DataTable

Dim rloop As Integer

dt = Part_Dataset.Tables(0)

For rloop = 0 To dt.Rows.Count - 1

Me.ComboBox1.Items.Add(dt.Rows(rloop).Item("PART"))

Next



Else

MessageBox.Show("Connection Failed", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)

End If

conn.Close()

conn.Dispose()

Catch ex As Exception

MessageBox.Show("Failed! " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)

End Try


Now I want to use this conn in FORM2 and FORM3 without having it to open same connection again. (FORM 1, 2, 3 are under same connection, but each form will use different table). Just open one connection in the main form (FORM1) and use it again again in other form.
 
In .NET 2,0, you dont manage the conenctions yourself.. Dont think that you can do a better job of it than Microsoft do, either :) - they have written their connection management code to work well with the pooling code built into the framework. Sharing your conenction and keeping it open breaks the pool and needlessly consumes resources

If youre opening your own connections, it's nearly guaranteed youre not doing something the right way..
 
there's another option:
take a module into ur project solution, open it and write the codes for the connection in there instead of form1. If everything goes fine then call form1. But there r two things to be remebered
1) Whatever variables u use for connection should be public
2) Never dispose the module as it contains the thread for that application

The code looks like this:
dim frm1 as new form1
declare other public variables here......

write the connectionstring....
Dim conn As New OracleConnection("Data Source=xxxx;User Id=xxxx;Password=xxxx;Integrated Security=no;")

Try
conn.Open()
application.run(frm1)
catch()
end try

Note the way u open form1 instead of writing- frm1.load() or frm1.show().
Now use this conn wherever u like. Hope this works....
 
there's another option:

On .NET 2, i really wouldnt follow that advice,sorry. An automated mechanism exists to generate for better, safer. faster code in seconds than you could do in days with your old approach.. If you bought a brand new car, would you then disconnect the Anti-Lock Brakes, Airbags, Seatbelt Pretensioners, Power Steering and Air Conditioning?
Probably not. So.. why would you buy VS2005, then carry on doing things the dinosaur ways of VB5, and not take advantage of any of the new, great features?
 
Back
Top