SqlConnectionManager.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Configuration;
  6. using System.Data.SqlClient;
  7. using System.Web;
  8. namespace Bowin.Common.Linq.DB
  9. {
  10. public class SqlConnectionManager : DbConnectionManager
  11. {
  12. public static void InitConnections()
  13. {
  14. CloseConnections();
  15. foreach (ConnectionStringSettings connectionSetting in ((ConnectionStringsSection)(ConfigurationManager.GetSection("connectionStrings"))).ConnectionStrings)
  16. {
  17. SqlConnection db = new SqlConnection(connectionSetting.ConnectionString);
  18. try
  19. {
  20. db.Open();
  21. }
  22. catch
  23. {}
  24. AddConnection(connectionSetting.Name, db);
  25. }
  26. IsGlobalConnectionStarted = true;
  27. }
  28. public static new SqlConnection GetConnection(string contextKey)
  29. {
  30. if (ExistsConnection(contextKey))
  31. {
  32. return (SqlConnection)DbConnectionManager.GetConnection(contextKey);
  33. }
  34. else
  35. {
  36. var connectionSetting = ((ConnectionStringsSection)(ConfigurationManager.GetSection("connectionStrings"))).ConnectionStrings[contextKey];
  37. SqlConnection db = new SqlConnection(connectionSetting.ConnectionString);
  38. try
  39. {
  40. db.Open();
  41. }
  42. catch
  43. { }
  44. AddConnection(connectionSetting.Name, db);
  45. return db;
  46. }
  47. }
  48. }
  49. }