Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RequestDatabaseFactory
- {
- public static SqlConnection Connection
- {
- get
- {
- if (HttpContext.Current.Items.Contains("RequestDatabaseFactoryConnection"))
- return HttpContext.Current.Items["RequestDatabaseFactoryConnection"] as SqlConnection;
- return null;
- }
- set
- {
- HttpContext.Current.Items["RequestDatabaseFactoryConnection"] = value;
- }
- }
- public static SqlConnection Get()
- {
- if (null == Connection)
- {
- Connection = new SqlConnection("connStringNameInWebconfig");
- Connection.Open();
- }
- return Connection;
- }
- public static void Close()
- {
- if (Connection != null)
- Connection.Close();
- }
- public static SqlCommand CreateCommand()
- {
- var ret= new SqlCommand();
- ret.Connection = Get();
- return ret;
- }
- }
- public class ComponentA
- {
- public object GetA()
- {
- var myCmd = RequestDatabaseFactory.CreateCommand();
- return null;
- }
- }
- public class ComponentB
- {
- public object GetB()
- {
- var myCmd = RequestDatabaseFactory.CreateCommand();
- return null;
- }
- }
- void main()
- {
- var ca = new ComponentA();
- var cb = new ComponentB();
- ca.GetA();
- cb.GetB();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement