SQLConnection passing w/ user controls

  • Thread starter Thread starter David Gilbert
  • Start date Start date
D

David Gilbert

Is better to pass the same SQL connection to all of my user controls.
Should I make a global object for my SQL connection, or is there some other
better choice out there.


Thanks

DG
 
David,

In my opinion is it simpler to make for your datahandling one or more
seperated classes.

The connection is than in that. The only things you are passing than is your
dataset (and what I do at the moment , the commands), the result can than be
error information. However there are a lot of ways to do this.

I hope this gives an idea

Cor
 
Ok just to put this in my own terms....

Create a class that has the instructions for my SQL connection and then open
and close, from there. This way If I change the Connection string, its only
in one place.

Thanks>...

DG


..... And the light bulb goes 'Click'
 
Ok last question on this...

Know of any good books/websites on Classes?
 
David,
Ok just to put this in my own terms....

Create a class that has the instructions for my SQL connection and then
open and close, from there. This way If I change the Connection string,
its only in one place.
No opening and closing is an unimportant part. When you use a dataadapter it
is even not needed.

However a sample, mostly typed, so watch typos or other things I typed
wrong.

\\\
Public Class DataClass
Public Function selectDS(mydataset, selectstring) As String
Dim frmSplash As New frmSplash
frmStatusMessage.Show("Connecting to SQL Server")
Dim conn As New SqlConnection(connString)
Try
Dim cmd As New SqlCommand (selectstring, conn)
da = New SqlDataAdapter(cmd)
dsMessages = New DataSet
da.Fill(mydataset)
Return ""
catch sqlExc As SqlException
return(sqlExc.ToString)
Catch ex As Exception
return(exc.Message)
Finally
conn.Close()
frmSplash.Close()
End Try
End Function
End Class
///

When you want it shared than the only thing that changes is
\\\
Public Shared Function selectDS(mydataset, selectstring) As String
///

I hope this gives some ideas

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top