Listbox Veri Ekleme Örneği(SQL Server İle)

Listbox Veri Ekleme Örneği(SQL Server İle) için tıklayınız.

Listbox Örneği

Listbox Örneği

 

 

 

 

 

 

 

 

 

 

Tablo

Tablo

 

 

 

 

 

 

 

Program Kodları:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace listbox_ornegi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection conn = new SqlConnection(“Data Source=.\\SQLEXPRESS; Initial Catalog=adres_defteri; Integrated Security=true;”);
private void button1_Click(object sender, EventArgs e)
{
if (conn.State == ConnectionState.Closed)
conn.Open();
string sorgu = “select * from kisiler”;
SqlDataAdapter adp = new SqlDataAdapter(sorgu,conn);
DataSet ds = new DataSet();
adp.Fill(ds);
for (int i = 0; i <= ds.Tables[0].Rows.Count – 1; i++)
{
listBox1.Items.Add(ds.Tables[0].Rows[i][0].ToString());
listBox2.Items.Add(ds.Tables[0].Rows[i][1].ToString());
listBox3.Items.Add(ds.Tables[0].Rows[i][2].ToString());
}
}
int secilen;
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
secilen = listBox1.SelectedIndex + 1;
if (conn.State == ConnectionState.Closed)
conn.Open();
string sorgu = “select telefonNumara from kisiler where id=”+secilen+””;
SqlDataAdapter adp = new SqlDataAdapter(sorgu, conn);
DataSet ds = new DataSet();
adp.Fill(ds);
textBox1.Text = ds.Tables[0].Rows[0][0].ToString();

}

private void button2_Click(object sender, EventArgs e)
{
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand(“update kisiler set telefonNumara='” + textBox1.Text + “‘ where id=” + secilen + “”, conn);
cmd.ExecuteNonQuery();
MessageBox.Show(“İşlem Tamam”);

}
}
}

 

 

admin