Tuesday, May 22, 2012

TESTING SYSTEM

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports DAL

Namespace DAL

    Public Class db

        ''' <summary>
        ''' Function return connection string with open state
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function OpenConnection() As SqlConnection

            SqlConnection.ClearAllPools()
            Dim str As String = ""
            str = ConfigurationManager.ConnectionStrings("con").ConnectionString
            Dim cn As New SqlConnection
            cn.ConnectionString = str
            cn.Open()
            Return cn


        End Function

        ''' <summary>
        ''' Function use the execute non query
        ''' </summary>
        ''' <param name="strQuery"></param>
        ''' <param name="strFunctionName"></param>
        ''' <remarks></remarks>
        Public Sub ExecuteNonQuery(ByVal strQuery As String, Optional ByVal strFunctionName As String = "")

            HttpContext.Current.Trace.Warn(strFunctionName & ":" & strQuery)
            Dim cn As New SqlConnection
            Dim cmd As New SqlCommand


            Try

                cn = OpenConnection()
                cmd.Connection = cn
                cmd.CommandType = CommandType.Text
                cmd.CommandText = strQuery
                cmd.ExecuteNonQuery()

            Finally

                cn.Close()

            End Try

        End Sub


        ''' <summary>
        ''' Function return dataset
        ''' </summary>
        ''' <param name="strQuery"></param>
        ''' <param name="strFunctionName"></param>
        ''' <remarks></remarks>
        Public Function FillDataset(ByVal strQuery As String, Optional ByVal strFunctionName As String = "") As DataSet

            HttpContext.Current.Trace.Warn(strFunctionName & ":" & strQuery)
            Dim cn As New SqlConnection
            Dim cmd As New SqlCommand
            Dim dst As New DataSet
            Dim da As New SqlDataAdapter

            Try

                cn = OpenConnection()
                cmd.Connection = cn
                cmd.CommandType = CommandType.Text
                cmd.CommandText = strQuery
                cmd.ExecuteNonQuery()
                da = New SqlDataAdapter(cmd)
                da.Fill(dst)

            Finally

                cn.Close()

            End Try

            Return dst

        End Function

        ''' <summary>
        ''' Function return dataset
        ''' </summary>
        ''' <param name="strQuery"></param>
        ''' <param name="strFunctionName"></param>
        ''' <remarks></remarks>
        Public Function FillDatareader(ByVal strQuery As String, Optional ByVal strFunctionName As String = "") As SqlDataReader

            HttpContext.Current.Trace.Warn(strFunctionName & ":" & strQuery)
            Dim cn As New SqlConnection
            Dim cmd As New SqlCommand
            Dim dr As SqlDataReader

            cn = OpenConnection()
            cmd.Connection = cn
            cmd.CommandType = CommandType.Text
            cmd.CommandText = strQuery
            dr = cmd.ExecuteReader()
            Return dr


        End Function


    End Class

End Namespace