VC#.Net Sql Veritabanındaki Bilgileri ListBox ta Gösterme
Bu makalemizde Sql veritabanımızdaki verileri Ad sütununa göre sorgulayıp, bulunan kayıtları listbox nesnemizde göstereceğiz. Formunuza 1 adet textBox, button,dataGridView ve listBox nesnesi ekleyin. Aşağıdaki şekilleri inceleyin...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnection baglan = new System.Data.SqlClient.SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\bilgiler.mdf;Integrated Security=True;User Instance=True");
System.Data.SqlClient.SqlDataAdapter adp = new System.Data.SqlClient.SqlDataAdapter();
adp.SelectCommand = new System.Data.SqlClient.SqlCommand("Select * From kisibilgi ", baglan);
System.Data.SqlClient.SqlCommandBuilder CB = new System.Data.SqlClient.SqlCommandBuilder(adp);
baglan.Open();
DataSet ds = new DataSet();
adp.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
baglan.Close();
}
private void btnAra_Click(object sender, EventArgs e)
{
if (txtAra.Text == "")
{
MessageBox.Show("Ad Giriniz..." + System.Environment.NewLine + "Tekrar Deneyiniz...Bahadirsa", "Bilgi Ekranı");
}
System.Data.SqlClient.SqlConnection baglan = new System.Data.SqlClient.SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\bilgiler.mdf;Integrated Security=True;User Instance=True");
System.Data.SqlClient.SqlDataAdapter adp=new System.Data.SqlClient.SqlDataAdapter();
adp.SelectCommand = new System.Data.SqlClient.SqlCommand("Select * From kisibilgi where ad like ‘%" + txtAra.Text + "%‘", baglan);
System.Data.SqlClient.SqlCommandBuilder CB = new System.Data.SqlClient.SqlCommandBuilder(adp);
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("Select * From kisibilgi where ad like ‘%" + txtAra.Text + "%‘" , baglan);
baglan.Open();
DataSet ds = new DataSet();
System.Data.SqlClient.SqlDataReader dr ;
adp.Fill(ds);
dr = cmd.ExecuteReader();
while (dr.Read()) {
dataGridView1.DataSource=ds.Tables[0];
listBox1.Items.Add(dr.GetString(0) + dr.GetString(1) + dr.GetString(2) + dr.GetString(3) + dr.GetString(4) + dr.GetString(5));
}
dr.Close();
baglan.Close();
}
}
}
//Bir sonraki makalede görüşmek üzere...Bahadirsa