code_junkie

Managing database connectivity with ADO.NET

Nov 12th, 2011
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. using( DbConnection dbConnection = GetDatabaseConnection() ) {
  2. doWork();
  3. }
  4.  
  5. DbConnection GetDatabaseConnection() {
  6. return GetConnection(MyConnectionString);
  7. }
  8.  
  9. DbConnection GetConnection(String connectionString)
  10. {
  11. try {
  12. SqlConnection dbConnection = new SqlConnection(connectionString);
  13. dbConnection.Open();
  14. return dbConnection;
  15. } catch( InvalidOperationException ex ) {
  16. handleError(ex);
  17. throw;
  18. } catch( DbException ex ) {
  19. handleError(ex);
  20. }
  21. }
Add Comment
Please, Sign In to add comment