nightrelax
New Member
Gurus, <BR><BR>Question: How do I make a connection available to numerous classes (objects)? (In other words, how can I make a connection object public in a webform so that all classes can access it?)<BR><BR>I have a webforms project (written in C#) that includes several <BR>classes--each of which retrieve or update data in a database. How can I make my connection object public so that it can be accessed by all my classes. Right now, I am getting an error that says my connection can not be accessed from within the classes. (The classess attempt to reference a global instance of the connection object and fail.)<BR><BR>I prefer not to open and close the connection with each instance of my classes. That seems extremely wasteful. Please help.<BR><BR>Anthony<BR><BR><BR><BR><BR><BR><BR><BR>Each time a pageYou can write a module and put it in there.. modules are global to your application.<BR><BR>And opening and closing a connection object *in* your classes is a better idea than just keeping it open the entire time.<BR><BR>From what I have read, connections should only be open when they are really needed.Does C# support modules?<BR><BR>I would prefer to only open the connection when I need it, but I need it several times in a row.<BR><BR>Should I simply open the connection, pass it into each class on load, and then close the connection when I am done loading all the classes? What are the pros and cons to this solution?<BR><BR>Anthonyi did something like this...<BR><BR>-------------------------------------------------<BR>Public Class UniversalDAL<BR> Public Function GetData(conn As IDbConnection, commandText As String) As IDataReader<BR> Dim cmd As IDbCommand = conn.CreateCommand()<BR> cmd.CommandText = commandText<BR> Try <BR> conn.Open()<BR> Catch<BR> ' the connection is already open or was invalid<BR> End Try<BR> <BR> Return cmd.ExecuteReader()<BR> End Function<BR>End class<BR> <BR>Public Class DataAccess<BR> Public Function GetDataReader(sSQL As String) As OleDbDataReader<BR> <BR> Dim uDAL As New UniversalDAL()<BR> Dim oledbConn As New OleDbConnection(ConfigurationSettings.AppSettings("ConnectionString"))<BR> <BR> Dim myDataReader As OleDbDataReader = uDAL.GetData(oledbConn,sSQL)<BR> <BR> Return myDataReader<BR> myDataReader.Close()<BR> oledbConn.Close()<BR> <BR> End Function<BR>End Class<BR>-------------------------------------------------<BR><BR>okay... what happens here is i have a universal data access class which utilized the IDbConnection interface which allows for OleDB, SQL, etc... the constructor for this class tries to open an instance of whatever connection you pass to it and returns an IDataReader. the DataAccess class is where you have all your data manipulation functions. for example, the GetDataReader() method is a generic method which returns a datareader. all you have to do is pass the sql string. you can also put update and insert functions in the DataAccess class which utilize the ExecuteNonQuery method (lemme know if you wanna look at that). <BR><BR>so... if you were to use this in your app, it would look something like this.<BR><BR>-------------------------------------------------<BR>Dim _data As New DataAccess()<BR>Dim sqlGetRecords As String = "Put your SQL statement here"<BR>Dim drRecords As OleDbDataReader = _data.GetDataReader(sqlGetRecords)<BR><BR>'do stuff with datareader here<BR><BR>drRecords.Close()<BR>-------------------------------------------------<BR><BR>hope this helps a little,<BR>../n1ck/