VB.NET DB Class

VB.NET DB Class

Birçok kişi vb.net ile nasıl database e bağlanırım diye sormuş işte bu işi yapan bir class sizlere. Bu class sayesinde codebehind da database üzerinde işlem yapabilirsiniz.

SQLClass.vb

'*********SQLClass.vb********
 
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.net.Mail
 
Public Class SQLClass
 
    Public strError As String = Nothing
    Private mConString As String = ""
 
    Public Property conString() As String
        Get
            Return mConString
        End Get
        Set(ByVal value As String)
            Conn = New SqlConnection(value)
            mConString = value
        End Set
    End Property
 
    Private Conn As SqlConnection
 
    Public Function GetDataTable(ByVal strSql As String) As DataTable
        Try
            Conn.Open()
            Dim da As New SqlDataAdapter(strSql, Conn)
            Dim dt As New DataTable
            da.Fill(dt)
 
            Conn.Close()
            Return dt
        Catch ex As Exception
            Conn.Close()
            strError = ex.Message
            Return Nothing
        End Try
    End Function
 
    Public Function GetDataSet(ByVal strSql As String) As DataSet
        Try
            Conn.Open()
            Dim da As New SqlDataAdapter(strSql, Conn)
            Dim ds As New DataSet
            da.Fill(ds)
 
            Conn.Close()
            Return ds
        Catch ex As Exception
            Conn.Close()
            strError = ex.Message
            Return Nothing
        End Try
    End Function
 
    Public Sub ExecuteQuery(ByVal strSql As String)
        Try
            Conn.Open()
            Dim cmd As New SqlCommand(strSql, Conn)
            cmd.ExecuteNonQuery()
 
            Conn.Close()
        Catch ex As Exception
            Conn.Close()
            strError = ex.Message
        End Try
    End Sub
    Public Function isConnected()
        Try
            Conn.Open()
            Conn.Close()
            Return True
        Catch ex As Exception
            Conn.Close()
            strError = ex.Message
            Return False
        End Try
    End Function
End Class

Kullanım

Imports System.Data
Partial Class OrnekSayfa
Inherits System.Web.UI.Page
    Dim sqlCls As SQLClass
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
                sqlYardimci = New SqlHelp()
                sqlYardimci.conString = CType(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"), String)
 
                Dim sqlCmd as String = "select * from TabloAdı"
                Dim dtOrnek As DataTable
                dtOrnek = sqlYardimci.GetDataTable(sqlCmd)
                Label1.Text = dtOrnek.Rows(0)("TablodakiAlan").ToString
 
    End Sub
End Class

Son Olarak da web.config dosyası

<appSettings>
 <add key="ConnectionString" value="server=127.0.0.1;database=dbadi;uid=kullanici;password=parola"/>
</appSettings>

Yorum yapılmamış »

Henüz yorum yapılmamış.

Bu yazıya yapılan yorumlar için RSS beslemeleri. Geri İzleme URL'si.

Yorum yapın