I have a VB.NET method, which connects to a database and reads the value. And this value is used multiple times in multiple places. The method looks like below:
Public Function IsConfigurationEnabled() As Integer
Dim IsEnabled As Integer
Dim sqlText As String
sqlText = "select value from [dbo].[Settings] where Configuration='XXX'"
Dim connection As SqlConnection = New SqlConnection()
Dim cmd As SqlCommand
connection.ConnectionString = "Data Source=localhost;Initial Catalog=XXX;Integrated Security=True"
Try
connection.Open()
cmd = New SqlCommand(sqlText, connection)
IsEnabled = Convert.ToInt32(cmd.ExecuteScalar())
cmd.Dispose()
connection.Close()
Catch ex As Exception
AuditTrail(vbLogEventTypeError, "IsConfigurationEnabled :Error opening SQL Connection ")
End Try
Return IsEnabled
End Function
I want to connect to database only once.
As the value in database never changes (or changes rarely).
Is there a way to achieve this?