C#- SQL Veritabanı Örneği(Listeleme-Arama-Ekleme)

C# İle SQL Server veritabanında arama, listeleme ve ekleme örneği için tıklayınız.

Arama-Listeleme-Ekleme

Arama-Listeleme-Ekleme

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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 VeritabaniGiris
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection conn = new SqlConnection(“Data Source=.\\SQLEXPRESS; Initial Catalog=ornek; Integrated Security=true;”);
private void button1_Click(object sender, EventArgs e)
{
// Random rnd = new Random();
conn.Open();
string sorgu = “select * from kimlik”;
SqlDataAdapter adp = new SqlDataAdapter(sorgu,conn);
DataSet ds = new DataSet();
adp.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
conn.Close();
}
int kontrolet(string TCKimlikNo)
{
conn.Open();
string sorgu = “select * from kimlik where TCKimlikNo='”+TCKimlikNo+”‘”;
SqlDataAdapter adp = new SqlDataAdapter(sorgu,conn);
DataSet ds = new DataSet();
adp.Fill(ds);
conn.Close();
if (ds.Tables[0].Rows.Count > 0)
{ return 0; }
else
{ return 1; }
}
private void button2_Click(object sender, EventArgs e)
{
int kontrolsonucu = kontrolet(textBox1.Text);
if (kontrolsonucu == 1)
{
conn.Open();
string sorgu = “insert into kimlik(TCKimlikNo,Ad,Soyad,DogumYer) values(‘” + textBox1.Text + “‘,'” + textBox2.Text + “‘,'” + textBox3.Text + “‘,'” + textBox4.Text + “‘) “;
SqlCommand cmd = new SqlCommand(sorgu, conn);
cmd.ExecuteNonQuery();
MessageBox.Show(“Kayıt Başarılı Şekilde Yapılmıştır…”);
conn.Close();
}
else
MessageBox.Show(“Aynı numaradan kayıtlı kişi var…”);

}

private void button3_Click(object sender, EventArgs e)
{
conn.Open();
string sorgu = “select * from kimlik where dogumyer='”+textBox5.Text+”‘”;
SqlDataAdapter adp = new SqlDataAdapter(sorgu, conn);
DataSet ds = new DataSet();
adp.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
conn.Close();
}

private void Form1_Load(object sender, EventArgs e)
{
conn.Open();
string sorgu = “select ad from kimlik”;
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++)
comboBox1.Items.Add(ds.Tables[0].Rows[i][0].ToString());
conn.Close();

}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
string sorgu = “select * from kimlik where ad='” + comboBox1.Text + “‘”;
SqlDataAdapter adp = new SqlDataAdapter(sorgu, conn);
DataSet ds = new DataSet();
adp.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
conn.Close();
}
}
}

 

 

 

 

 

 

 

 

admin