Current Path : G:/PleskVhosts/mpcdp.in/cmamp.mpcdp.in/cdpp3/App_Code/DAL/ |
Windows NT SG2NWVPWEB022 10.0 build 17763 (Windows Server 2016) i586 |
Current File : G:/PleskVhosts/mpcdp.in/cmamp.mpcdp.in/cdpp3/App_Code/DAL/dalGeneral.vb |
'Imports Microsoft.VisualBasic 'Public Class dalGeneral 'End Class Imports System.Collections.Generic Imports System.Web Imports System.Data Imports System.Data.SqlClient Imports System.Configuration ''' <summary> ''' Summary description for dalGeneral ''' </summary> Public Class dalGeneral ' ' TODO: Add constructor logic here ' Public Sub New() End Sub End Class Public Class Command Private Connection As SqlConnection Private sqlCommand As SqlCommand Private dataReader As SqlDataReader Private Shared ConnectionString As [String] = General.GetAppSetting("ConnectionString") Private _Closed As Boolean = True Public Sub New(ByVal CommandText As String, ByVal isStoredProcedure As Boolean) Connection = New SqlConnection(ConnectionString) sqlCommand = New SqlCommand(CommandText, Connection) ' sqlCommand.CommandType = If(isStoredProcedure, CommandType.StoredProcedure, CommandType.Text) ' If sqlCommand.CommandType = isStoredProcedure Then If isStoredProcedure = True Then sqlCommand.CommandType = CommandType.StoredProcedure Else sqlCommand.CommandType = CommandType.Text End If sqlCommand.CommandTimeout = 300 End Sub Public ReadOnly Property Reader() As SqlDataReader Get Return dataReader End Get End Property Public ReadOnly Property Parameters() As SqlParameterCollection Get Return sqlCommand.Parameters End Get End Property Public Shared Function CreateSqlConnection() As SqlConnection Return New SqlConnection(ConnectionString) End Function Public Property CommandText() As String Get Return sqlCommand.CommandText End Get Set(ByVal value As String) sqlCommand.CommandText = value End Set End Property Public Shared Function IsConnectionOK() As Boolean Dim Result As Boolean = False Try ' Note: Pooling must be set to false otherwise Connection ' will be draws from the pool and result will be true even ' when the sql server is down/disconnected Dim Connection As New SqlConnection(ConnectionString & ";Pooling=false") Connection.Open() Connection.Close() Result = True Catch End Try Return Result End Function Public Shared Sub Open(ByVal Connection As SqlConnection) Try Connection.Open() Catch ' TODO: - 1 Log connection fail records Try Connection.ConnectionString = ConnectionString & ";Pooling=false" Connection.Open() Catch Connection.ConnectionString = ConnectionString & ";Pooling=false" Connection.Open() End Try End Try End Sub Public Sub Open() Dim ConnectionCount As Integer = 0 If System.Web.HttpContext.Current.Items("ConnectionCount") IsNot Nothing Then ConnectionCount = CInt(System.Web.HttpContext.Current.Items("ConnectionCount")) End If ' Connection.Open(); Open(Connection) ConnectionCount += 1 System.Web.HttpContext.Current.Items("ConnectionCount") = ConnectionCount _Closed = False End Sub Public Sub Close() If dataReader IsNot Nothing AndAlso Not dataReader.IsClosed Then dataReader.Close() End If Connection.Close() If Not _Closed Then _Closed = True If System.Web.HttpContext.Current.Items("ConnectionCount") IsNot Nothing Then Dim ConnectionCount As Integer = CInt(System.Web.HttpContext.Current.Items("ConnectionCount")) ConnectionCount -= 1 System.Web.HttpContext.Current.Items("ConnectionCount") = ConnectionCount End If End If End Sub Public Sub Prepare() Try ' sqlCommand.Connection.Open(); Open(sqlCommand.Connection) sqlCommand.Prepare() Finally sqlCommand.Connection.Close() End Try End Sub Public Function ExecuteNonQuery() As Integer Dim Result As Integer = -1 Try ' Connection.Open(); Open(Connection) Result = sqlCommand.ExecuteNonQuery() Finally Connection.Close() End Try Return Result End Function Public Function ExecuteDataSet() As DataSet Dim ds As New DataSet() Try Dim da As New SqlDataAdapter(sqlCommand) Open(Connection) da.Fill(ds) Finally Connection.Close() End Try Return ds End Function Public Shared Function ExecuteNonQuery(ByVal SqlText As String) As Integer Dim Result As Integer = -1 Dim sqlCommand As New SqlCommand(SqlText, New SqlConnection(ConnectionString)) Try ' sqlCommand.Connection.Open(); Open(sqlCommand.Connection) Result = sqlCommand.ExecuteNonQuery() Finally sqlCommand.Connection.Close() End Try Return Result End Function Public Function ExecuteReader(ByVal SingleResult As Boolean, ByVal SingleRow As Boolean) As Boolean Dim behavior As CommandBehavior = CommandBehavior.CloseConnection If SingleResult Then behavior = behavior Or CommandBehavior.SingleResult If SingleRow Then behavior = behavior Or CommandBehavior.SingleRow End If End If Open() dataReader = sqlCommand.ExecuteReader(behavior) Return dataReader.HasRows End Function Public Function ExecuteScalar() As Object Dim Result As Object Try ' Connection.Open(); Open(Connection) Result = sqlCommand.ExecuteScalar() Finally Connection.Close() End Try Return Result End Function Public Shared Function ExecuteScalar(ByVal SqlText As String) As Object Dim Result As Object Dim sqlCommand As New SqlCommand(SqlText, New SqlConnection(ConnectionString)) Try ' sqlCommand.Connection.Open(); Open(sqlCommand.Connection) Result = sqlCommand.ExecuteScalar() Finally sqlCommand.Connection.Close() End Try Return Result End Function Public Function GetString(ByVal FieldName As String) As String Dim i As Integer = dataReader.GetOrdinal(FieldName) If Not dataReader.IsDBNull(i) Then Return dataReader.GetString(i) Else Return String.Empty End If End Function Public Function GetInt(ByVal FieldName As String) As Integer Dim i As Integer = dataReader.GetOrdinal(FieldName) If Not dataReader.IsDBNull(i) Then Return dataReader.GetInt32(i) Else Return 0 End If End Function Public Function GetShort(ByVal FieldName As String) As Short Dim i As Integer = dataReader.GetOrdinal(FieldName) If Not dataReader.IsDBNull(i) Then Return dataReader.GetInt16(i) Else Return 0 End If End Function Public Function GetByte(ByVal FieldName As String) As Byte Dim i As Integer = dataReader.GetOrdinal(FieldName) If Not dataReader.IsDBNull(i) Then Return dataReader.GetByte(i) Else Return 0 End If End Function Public Function GetBytes(ByVal FieldName As String, ByVal length As Integer) As Byte() Dim Result As Byte() = New Byte(length - 1) {} Dim i As Integer = dataReader.GetOrdinal(FieldName) If Not dataReader.IsDBNull(i) Then dataReader.GetBytes(i, 0, Result, 0, length) End If Return Result End Function Public Function GetDecimal(ByVal FieldName As String) As Decimal Dim i As Integer = dataReader.GetOrdinal(FieldName) If Not dataReader.IsDBNull(i) Then Return dataReader.GetDecimal(i) Else Return 0 End If End Function Public Function GetDouble(ByVal FieldName As String) As Double Dim i As Integer = dataReader.GetOrdinal(FieldName) If Not dataReader.IsDBNull(i) Then Return dataReader.GetDouble(i) Else Return 0 End If End Function Public Function GetFloat(ByVal FieldName As String) As Single Dim i As Integer = dataReader.GetOrdinal(FieldName) If Not dataReader.IsDBNull(i) Then Return dataReader.GetFloat(i) Else Return 0 End If End Function Public Function GetDateTime(ByVal FieldName As String) As DateTime Dim i As Integer = dataReader.GetOrdinal(FieldName) If Not dataReader.IsDBNull(i) Then Return dataReader.GetDateTime(i) Else Return DateTime.MinValue End If End Function Public Function GetBoolean(ByVal FieldName As String) As Boolean Dim i As Integer = dataReader.GetOrdinal(FieldName) If Not dataReader.IsDBNull(i) Then Return dataReader.GetBoolean(i) Else Return False End If End Function Public Function GetValue(ByVal FieldName As String) As Object Dim i As Integer = dataReader.GetOrdinal(FieldName) If Not dataReader.IsDBNull(i) Then Return dataReader.GetValue(i) Else Return DBNull.Value End If End Function Public Shared Sub SetConnectionString(ByVal sqlDataSource As System.Web.UI.WebControls.SqlDataSource) sqlDataSource.ConnectionString = ConnectionString End Sub End Class Public Enum Tables Inventory = 1 End Enum Public Enum ResultType Successful UniqueConstraintError OtherSqlError Unknown End Enum