Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.35 KB | None | 0 0
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
  2. Dim dp As New DataProxy
  3. Dim listOfObs As New List(Of MyObject)
  4. dim someId as integer = 1
  5. Try
  6. If Not Page.IsPostBack Then
  7. listOfObs = dp.ExampleReadFuncion(someId)
  8. End If
  9. Catch ex As Exception
  10. Throw
  11. Finally
  12. dp.dispose()
  13. dp = Nothing
  14. SetMenue()
  15. End Try
  16. End Sub
  17.  
  18. Public Class DataProxy
  19. Dim scConAdapter As New ConnectionAdapter
  20.  
  21. Public Sub New()
  22. Try
  23. scConAdapter.Connect()
  24. Catch ex As Exception
  25. Throw
  26. End Try
  27. End Sub
  28.  
  29. Public Sub dispose()
  30. scConAdapter.Dispose()
  31. End Sub
  32. Private Shared Sub Main()
  33. End Sub
  34. Public Function getDBConnection() As SqlConnection
  35. Dim conn As New ConnectionAdapter
  36. Return conn.getDBConnection
  37. End Function
  38. Public Function getDBConnectionString() As String
  39. Dim conn As New ConnectionAdapter
  40. Return conn.getDBConnectionString
  41. End Function
  42.  
  43. Public Function ExampleReadFuncion(ByVal someId As Integer) As List(Of MyObject)
  44. Dim successFactor As LogStatusEnum = LogStatusEnum.INFO
  45. Dim newEx As Exception = Nothing
  46. Dim conn As New ConnectionAdapter
  47. Dim myObj As ActivityMarker
  48. Dim listOfObs As New List(Of MyObject)
  49. Dim dr As SqlDataReader = Nothing
  50.  
  51. Try
  52. successFactor = LogStatusEnum.INFO
  53. conn.Connect()
  54. dr = conn.ExampleReadFuncion(someId)
  55. Using dr
  56. If (dr.HasRows = True) Then
  57. While dr.Read
  58. myObj = New myObj
  59. myObj.Marker_Id = dr.Item("id")
  60. myObj.Acitvity_Id = dr.Item("someValue")
  61.  
  62. listOfObs.Add(myObj)
  63. End While
  64. End If
  65. End Using
  66. Return listOfObs
  67. Catch ex As Exception
  68. successFactor = LogStatusEnum.ERRORS
  69. Throw
  70. Finally
  71. dr.Close()
  72. dr = Nothing
  73. conn.Dispose()
  74. conn = Nothing
  75. End Try
  76. End Function
  77.  
  78. end class
  79.  
  80. Public Class ConnectionAdapter
  81. Inherits SqlConnectionAdapter
  82.  
  83. Public Sub New()
  84. End Sub
  85.  
  86. Public Function ExampleReadFuncion(ByVal someId As Integer) As SqlDataReader
  87. Try
  88. Dim dr As SqlDataReader = Nothing
  89. Dim selectString As New StringBuilder
  90. Dim cmd As SqlCommand = Nothing
  91. Try
  92. cmd = CreateCommand()
  93.  
  94. selectString.Append("SELECT * " & vbCrLf)
  95. selectString.Append("FROM " & vbCrLf)
  96. selectString.Append("dbo.mytable " & vbCrLf)
  97. selectString.Append("WHERE " & vbCrLf)
  98. selectString.Append("id = @SOME_ID " & vbCrLf)
  99.  
  100. With cmd
  101. .CommandType = CommandType.Text
  102. .CommandText = selectString.ToString
  103. .Parameters.Add("@SOME_ID", SqlDbType.Int).Value = someId
  104.  
  105. dr = .ExecuteReader
  106. End With
  107.  
  108. Catch ex As Exception
  109. Throw
  110. Finally
  111. cmd.Dispose()
  112. End Try
  113. Return dr
  114. Catch ex As Exception
  115. Throw ex
  116. End Try
  117. End Function
  118. end class
  119.  
  120. Public MustInherit Class SqlConnectionAdapter
  121. Protected CurrentTransaction As SqlTransaction
  122. Public Property db As SqlConnection
  123. Public Property Password As String
  124. Public Property TNSName As String
  125. Public Property User As String
  126. Public Property DBName As String
  127. Public Property PortNumber As Integer
  128.  
  129.  
  130. Public Overridable Sub Dispose()
  131. Try
  132. If Not CurrentTransaction Is Nothing Then
  133. CurrentTransaction.Commit()
  134. End If
  135.  
  136. Catch ex As Exception
  137. Throw
  138. Finally
  139. If Not db Is Nothing Then
  140. db.Close()
  141. db.Dispose()
  142. db = Nothing
  143. End If
  144. End Try
  145. End Sub
  146.  
  147. Public Overridable Sub Connect()
  148. Try
  149. Dim appSettings = ConfigurationManager.AppSettings
  150.  
  151. If (appSettings("releaseVersion") = "DEBUG") Then
  152. Connect(appSettings("db_sqlHost"), appSettings("db_sqlDb"))
  153. Else
  154. Connect(appSettings("db_sqlHost"), appSettings("db_sqlPort"), appSettings("db_sqlDb"), appSettings("db_sqlUser"), appSettings("db_sqlPassword"))
  155. End If
  156.  
  157. Catch ex As Exception
  158. Throw
  159. End Try
  160. End Sub
  161.  
  162. Public Sub Connect(ByVal GetServername As String, ByVal GetDatabaseName As String)
  163. Try
  164. TNSName = GetServername
  165. DBName = GetDatabaseName
  166.  
  167. db = New SqlConnection
  168. db = SqlConnectionUtilities.GetConnection(GetServername, GetDatabaseName)
  169. Catch ex As Exception
  170. Throw
  171. End Try
  172. End Sub
  173. Public Sub Connect(ByVal GetServerName As String, ByVal GetPort As Long, ByVal GetDatabase As String, ByVal GetUsername As String, ByVal Getpassword As String)
  174. Try
  175. User = GetUsername
  176. Password = Getpassword
  177. PortNumber = GetPort
  178. DBName = GetDatabase
  179. TNSName = GetServerName
  180.  
  181. db = New SqlConnection
  182. db = SqlConnectionUtilities.GetConnection(GetServerName, GetPort, GetDatabase, GetUsername, Getpassword)
  183. Catch ex As Exception
  184. Throw
  185. End Try
  186. End Sub
  187.  
  188. Protected Function CreateCommand() As SqlCommand
  189. Dim ret As SqlCommand = Nothing
  190.  
  191. Try
  192. ret = db.CreateCommand
  193.  
  194. If Not CurrentTransaction Is Nothing Then
  195. ret.Transaction = CurrentTransaction
  196. End If
  197. Catch ex As Exception
  198. Throw
  199. Finally
  200.  
  201. End Try
  202. Return ret
  203. End Function
  204. Public Sub BeginTransaction()
  205. If CurrentTransaction Is Nothing Then
  206. CurrentTransaction = db.BeginTransaction
  207. End If
  208. End Sub
  209. Public Sub CommitTransaction()
  210. If Not CurrentTransaction Is Nothing Then
  211. CurrentTransaction.Commit()
  212. CurrentTransaction.Dispose()
  213. CurrentTransaction = Nothing
  214. End If
  215. End Sub
  216. Public Sub RollbackTransaction()
  217. If Not CurrentTransaction Is Nothing Then
  218. CurrentTransaction.Rollback()
  219. CurrentTransaction.Dispose()
  220. CurrentTransaction = Nothing
  221. End If
  222. End Sub
  223. Protected Overrides Sub Finalize()
  224. MyBase.Finalize()
  225. End Sub
  226. End Class
  227.  
  228. Public Class SqlConnectionUtilities
  229.  
  230. Public Shared Property connectionString As String
  231.  
  232. Public Shared Function GetConnection(ByVal ServerName As String, ByVal Port As String, ByVal Database As String, ByVal username As String, ByVal password As String) As SqlConnection
  233. Dim connString As New StringBuilder
  234. Dim con As SqlConnection
  235. Try
  236. connString.Append("Server=tcp:" & ServerName & "," & Port & ";")
  237. connString.Append("Initial Catalog = " & Database & "; Persist Security Info=False;")
  238. connString.Append("User ID = " & username & ";")
  239. connString.Append("Password = " & password & ";")
  240. connString.Append("MultipleActiveResultSets = False;")
  241. connString.Append("Encrypt = True;TrustServerCertificate=False;Connection Timeout=30;")
  242.  
  243. connectionString = connString.ToString
  244.  
  245. con = New SqlConnection(connString.ToString)
  246. con.Open()
  247. Return con
  248. Catch ex As Exception
  249. Throw
  250. End Try
  251. End Function
  252.  
  253. Public Shared Function GetConnection(ByVal Servername As String, ByVal DatabaseName As String) As SqlConnection
  254. Dim ConnectString As String
  255. Dim con As SqlConnection
  256. Try
  257. ConnectString = "Data Source=" & Servername & ";Initial Catalog=" & DatabaseName & ";Integrated Security=True"
  258. connectionString = ConnectString
  259. con = New SqlConnection(ConnectString)
  260. con.Open()
  261. Return con
  262. Catch ex As Exception
  263. Throw
  264. End Try
  265. End Function
  266. end class
  267.  
  268. SELECT
  269. DB_NAME(dbid) as DBName,
  270. COUNT(dbid) as NumberOfConnections,
  271. loginame as LoginName
  272. FROM
  273. sys.sysprocesses
  274. WHERE
  275. dbid > 0
  276. GROUP BY
  277. dbid, loginame
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement