kannan s

kannan s

  • NA
  • 77
  • 0

Reg :Database connection class file for ASP.net 2005

Feb 18 2008 9:43 AM

Imports Microsoft.VisualBasic

Imports System.Data

Imports System.Configuration

Namespace MyDatabase

Public Class DataBase

Dim Ds As DataSet

Dim Sdr As IDataReader

Dim Cnstr As String

Dim SqlDa As SqlDataSource

Public Sub New()

Cnstr = ConfigurationManager.ConnectionStrings("Constr").ConnectionString

SqlDa = New SqlDataSource()

SqlDa.ConnectionString = Cnstr

SqlDa.ProviderName = ConfigurationManager.ConnectionStrings("Constr").ProviderName

End Sub

Public Sub GetDataset(ByVal Sql As String, ByRef Dset As DataSet)

Dim Dv As New DataView

Dim Dt As New DataTable

Try

Dset = New DataSet

SqlDa.SelectCommand = Sql

SqlDa.DataSourceMode = SqlDataSourceMode.DataSet

Dv = CType(SqlDa.Select(DataSourceSelectArguments.Empty), DataView)

Dt = Dv.ToTable

Dset.Tables.Add(Dt)

Catch ex As Exception

Finally

If Not SqlDa Is Nothing Then SqlDa.Dispose()

If Not Dv Is Nothing Then Dv.Dispose()

End Try

End Sub

Public Sub Execute(ByVal Sql As String)

Try

If Sql.ToUpper.StartsWith("INSERT") Then

SqlDa.InsertCommand = Sql

SqlDa.Insert()

ElseIf Sql.ToUpper.StartsWith("UPDATE") Then

SqlDa.UpdateCommand = Sql

SqlDa.Update()

ElseIf Sql.ToUpper.StartsWith("DELETE") Then

SqlDa.DeleteCommand = Sql

SqlDa.Delete()

End If

Catch ex As Exception

Finally

If Not SqlDa Is Nothing Then SqlDa.Dispose()

End Try

End Sub

Public Sub Close()

If Not SqlDa Is Nothing Then SqlDa.Dispose()

If Not SqlDa Is Nothing Then SqlDa = Nothing

End Sub

Public Sub GetDataReader(ByVal Sql As String, ByRef Dr As IDataReader)

Dim Dv As New DataView

Dim Dt As New DataTable

Try

SqlDa.SelectCommand = Sql

SqlDa.DataSourceMode = SqlDataSourceMode.DataReader

Dr = CType(SqlDa.Select(DataSourceSelectArguments.Empty), IDataReader)

Catch ex As Exception

Finally

If Not SqlDa Is Nothing Then SqlDa.Dispose()

If Not SqlDa Is Nothing Then SqlDa = Nothing

If Not Dv Is Nothing Then Dv.Dispose()

If Not Dt Is Nothing Then Dt.Dispose()

End Try

End Sub

Public Function GetMax(ByVal Sql As String) As Double

Dim Dv As New DataView

Dim RecCount As Double = 0

Try

SqlDa.SelectCommand = Sql

SqlDa.DataSourceMode = SqlDataSourceMode.DataSet

Dv = CType(SqlDa.Select(DataSourceSelectArguments.Empty), DataView)

RecCount = Dv.Count + 1

Catch ex As Exception

Finally

If Not SqlDa Is Nothing Then SqlDa.Dispose()

If Not SqlDa Is Nothing Then SqlDa = Nothing

If Not Dv Is Nothing Then Dv.Dispose()

End Try

Return RecCount

End Function

Public Function GetValue(ByVal Sql As String) As Object

Dim Dv As New DataView

Dim Dt as New DataTable

Dim Result As Object = ""

Try

SqlDa.SelectCommand = Sql

SqlDa.DataSourceMode = SqlDataSourceMode.DataSet

Dv = CType(SqlDa.Select(DataSourceSelectArguments.Empty), DataView)

Result = Dv.Table.Rows(0).Item(0)

Catch ex As Exception

Finally

If Not SqlDa Is Nothing Then SqlDa.Dispose()

If Not SqlDa Is Nothing Then SqlDa = Nothing

If Not Dv Is Nothing Then Dv.Dispose()

If Not Dt Is Nothing Then Dt.Dispose()

End Try

Return Result

End Function

End Class

End Namespace