K
Krissy
Jonathan Simms said:This is probably a dumb question:
In a web application, say a shopping site, is it best to have global
SqlConnection and data adapters that all pages share, or should each page
define it's own connection? How about DataSets?
I find it is easier to put a connection in the web config, that way you dont
have to declare it on each page!
Also, in the future, if you need to change your connection to another
database, you only need to change one instance of the connection, and not on
each page.
Example:
In the web.config under the <configuration> tags, I have:
<appSettings>
<add key="SQLConn" value="server=192.168.1.1;user
id=userl;password=pass;initial catalog=mydb" />
</appSettings>
I then have a library file, ( library.vb) which I have the following code:
(MyLib = the class name of the library)
' ******************** Database connection information
*********************
Public Const DBSource As MyLib.DBConn = MyLib.DBConn.SQLConn
Public Shared Function GetDBConn(ByVal MyConn As DBConn) As
SqlClient.SqlConnection
Return New
SqlClient.SqlConnection(ConfigurationSettings.AppSettings(MyConn.ToString))
End Function
'** You can put here the name of your connection(s) from the web.config
Public Enum DBConn
SQLConn
End Enum
And then, in my code, if I want to call the connection:
Dim sqlcmd As New SqlCommand("", MyLib.GetDBConn(MyLib.DBSource))