Asp.Net' de Class dosyasini .aspx uzantili sayfamiza dahil ederek, Class dosyamizin içerisindeki metodlari kullanarak veri tabani islemleri ile örnekler... Yararli olmasi dilegiyle...
VeriTabani.vb Class Dosyamiz...
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.OleDb
Public
Class veribaglanti
Public Function baglan() As OleDbConnection
Dim yol As String = System.Web.HttpContext.Current.Server.MapPath("App_Data/vt1.mdb") ' Veritabani yolumuzu belirtiyoruz...
Dim baglanti As New OleDbConnection("Provider=Microsoft.jet.OleDb.4.0; Data Source=" + yol) ' Connection Nesnesi tanimliyoruz ve kullanilacak provider ve veritabani yolunu belirliyoruz...
Return baglanti ' Baglanti nesnemizi gönderiyoruz...
End Function
Public Function verikayit(Sqlcumle As String, cmdType As CommandType, OldbType1 As OleDbType, OldbType2 As OleDbType, deger1 As String, deger2 As Integer) As Integer ' Fonkisyonumuzu ve degerlerimizi tanimliyoruz...
Dim baglanti As OleDbConnection = Me.baglan() ' Yukaridaki baglan fonksiyonumuzu dahil ediyoruz...
Dim sorgu As New OleDbCommand(Sqlcumle, baglanti) ' OledbCommand nesnemizi tanimlayip sql cümle sorgusu için stringimizi ve baglantimizi dahil ediyoruz...
sorgu.Parameters.Add("?", OldbType1, 35).Value = deger1 ' Parametre ve degerlerimizi ekliyoruz...
sorgu.Parameters.Add("?", OldbType2, 30).Value = deger2 ' Parametre ve degerlerimizi ekliyoruz...
sorgu.Connection = baglanti 'Command nesnesini Connection nesnesiyle iliskilendiriyorz...
Dim sonuc As Integer = 0
Try ' Hiç bir sikinti olmadan islemimiz çalismis ise Try araligi çalisacak
baglanti.Open()
sonuc = sorgu.ExecuteNonQuery()
Catch ex As OleDbException ' Eger problem varsa Catch bölümü çalisacak...
Throw New Exception(ex.Message) ' Hata mesajini ekrana yazdiracak...
End Try
sorgu.Dispose()
baglanti.Close()
baglanti.Dispose()
Return (sonuc) ' Geriye sonuç degiskenini döndürecek...
End Function
Public Function OledbCommand(Sqlcumle As String) As Integer 'OledbCommand Nesnemizi tanimliyoruz...
Dim baglanti As OleDbConnection = Me.baglan()
Dim sorgu As New OleDbCommand(Sqlcumle, baglanti)
sorgu.Connection = baglanti
Dim sonuc As Integer = 0
Try
baglanti.Open()
sorgu.ExecuteNonQuery()
Return sonuc
Catch ex As OleDbException
Throw New Exception(ex.Message)
End Try
sorgu.Dispose()
baglanti.Close()
baglanti.Dispose()
End Function
Public Function GetDataTable(SqlCumle As String) As DataTable 'Datatable nesnemiz...
Dim baglanti As OleDbConnection = Me.baglan
Dim Adapter As New OleDbDataAdapter(SqlCumle, baglanti)
Dim DataTable As New DataTable
Dim sonuc As Integer = 0
Try
baglanti.Open()
Adapter.Fill(DataTable)
Catch ex As OleDbException
Throw New Exception(ex.Message)
End Try
Adapter.Dispose()
baglanti.Dispose()
baglanti.Close()
Return DataTable
End Function
Public Function GetDataSet(SqlCumle As String) As DataSet 'DataSet nesnemiz
Dim baglanti As OleDbConnection = Me.baglan
Dim Adapter As New OleDbDataAdapter(SqlCumle, baglanti)
Dim DataSet As New DataSet
Dim sonuc As Integer = 0
Try
baglanti.Open()
Adapter.Fill(DataSet)
Catch ex As OleDbException
Throw New Exception(ex.Message)
End Try
Adapter.Dispose()
baglanti.Dispose()
baglanti.Close()
Return DataSet
End Function
Public Function DataRow(SqlCumle As String) As DataRow 'DataRow nesnemiz
Dim DataTable As DataTable = GetDataTable(SqlCumle)
If DataTable.Rows.Count = 0 Then
Return Nothing
Else
Return DataTable.Rows(0)
End If
End Function
Public Function GetDataCell(SqlCumle As String) As String
Dim DataTable As DataTable = GetDataTable(SqlCumle)
If DataTable.Rows.Count = 0 Then
Return Nothing
Else
Return DataTable.Rows(0)(1)
End If
End Function
Public Function kayit(SqlString As String, kullanici As String, parola As String) As Integer
Dim baglanti As OleDbConnection = Me.baglan
Dim sorgu As New OleDbCommand(SqlString, baglanti)
sorgu.Parameters.AddWithValue("?", kullanici)
sorgu.Parameters.AddWithValue("?", parola)
Dim sonuc As Integer = 0
Try
baglanti.Open()
sonuc = sorgu.ExecuteNonQuery
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
Return sonuc
sorgu.Dispose()
baglanti.Close()
baglanti.Dispose()
End Function
End Class
.aspx sayfamizda kullanbileceginiz kodlarimiz
Public Function verikayit(Sqlcumle As String, OldbType1 As OleDbType, OldbType2 As OleDbType, deger1 As String, deger2 As Integer) As Integer Fonksiyonu
class1.verikayit("insert into Tbl_Kullanici (kullanici,sifre) values (?,?)", CommandType.Text, OleDbType.Char, OleDbType.Integer, TextBox1.Text, TextBox2.Text)
Public Function kayit(SqlString As String, kullanici As String, parola As String) As Integer Fonksiyonu
class1.kayit("insert into Tbl_Kullanici (kullanici,sifre) values (?,?)", TextBox1.Text, TextBox2.Text)
Public Function kayit(SqlString As String, kullanici As String, parola As String) As Integer Fonksiyonu
komut.CommandText ="insert into Tbl_Kullanici (kullanici,sifre,mail) values (?,?,?)"
Public Function DataRow(SqlCumle As String) As DataRow Fonksiyonu
Dim tablo As DataTable = class1.GetDataTable("Select * From Tbl_Kullanici")
For i As Integer = 1 To tablo.Rows.Count
Dim dr As DataRow = tablo.Rows(0)
Response.Write(dr("kullanici").ToString + "
")
Next
Public Function GetDataCell(SqlCumle As String) As String Fonksiyonu
Dim uye As String = ""
uye = class1.GetDataCell("select * from Tbl_Kullanici")
Response.Write(uye)
Bir örnek yapacak olursak ve veritabanimizdaki bilgileri DataListe Class dosyamizdaki olusturmuz Fonkiyon ile çagirmak istersek :
Dim tablo As DataTable = class1.GetDataTable("Select * From Tbl_Kullanici")
DataList1.DataSource = tablo
DataList1.DataBind()
sadece 3 satir ile dosyalarimizi çekebiliriz !!!