DataReader ile TextBox a Veri Çekme
Evet arkadaşlar Datareader kullanarak Serverımızdaki sql veritabanımızdan Textbox ımıza veri çekeceğiz. Unutmadan TextBox ımızın MultiLine özelliğini True yapın...Bahadirsa
Aşağıdaki şekildeki gibi formunuzu tasarlayın. Formunuza 1 adet TextBox ve Button ekleyin.
TextBox a bilgileri datareader ile yapacağız. İstenilen sütundaki bilgileri GetString metodu kullanarak getireceğiz.
Şekil 1
Şekil 2
Forma yazılacak kodlar:
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Form1
Inherits Windows.Forms.Form
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With Me
.Text = "DataReader...Bahadirsa"
.MaximizeBox = False
.Size = New System.Drawing.Size(174, 182)
.Location = New Point(350, 200)
End With
Button1.Text = "Veri Al"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim baglan As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\bilgiler.mdf;Integrated Security=True;User Instance=True")
Dim cmd As New SqlClient.SqlCommand("Select * From kisibilgi", baglan)
baglan.Open()
Dim rd As Data.SqlClient.SqlDataReader
Dim ds1 As New DataSet()
rd = cmd.ExecuteReader
While rd.Read
TextBox1.Text = TextBox1.Text & rd.GetString(0) & ControlChars.CrLf
End While
rd.Close()
baglan.Close()
End Sub
End Class
Forma yazılacak
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Form1
Inherits Windows.Forms.Form
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With Me
.Text = "DataReader...Bahadirsa"
.MaximizeBox = False
.Size = New System.Drawing.Size(174, 182)
.Location = New Point(350, 200)
End With
Button1.Text = "Veri Al"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim baglan As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\bilgiler.mdf;Integrated Security=True;User Instance=True")
Dim cmd As New SqlClient.SqlCommand("Select * From kisibilgi", baglan)
baglan.Open()
Dim rd As Data.SqlClient.SqlDataReader
Dim ds1 As New DataSet()
rd = cmd.ExecuteReader
While rd.Read
TextBox1.Text = TextBox1.Text & rd.GetString(0) & ControlChars.CrLf
End While
rd.Close()
baglan.Close()
End Sub
End Class
‘Bir sonraki makalede buluşmak üzere...Bahadirsa