SqlConnectionManager.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 (IsGlobalConnectionStarted)
  31. {
  32. return (SqlConnection)DbConnectionManager.GetConnection(contextKey);
  33. }
  34. else
  35. {
  36. return null;
  37. }
  38. }
  39. }
  40. }