C# access bağlantısı ile basit veri tabanı işlemleri(Ekle, Sil, Ara, Güncelle) yapalım form üzerindeki nesnelerimiz gibi oluşturduktan sonra kodlarımız aşağıdaki örnek ile aynı düzenleyelim.
[code]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.OleDb;
namespace veritabani_islemleri
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OleDbConnection connect = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=database.accdb");
OleDbCommand cmd = new OleDbCommand();
OleDbDataReader read;
private void linkLabel1_LinkClicked_1(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("www.cinaraktas.com.tr");
}
private void Temizle()
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
}
private void button1_Click_1(object sender
, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
{
MessageBox.Show("Tüm alanları doldurunuz.", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
connect.Open();
cmd.Connection = connect;
cmd.CommandText = ("INSERT INTO bilgiler(tc_no,adi,soyadi) VALUES(‘" + textBox1.Text + "’,’" + textBox2.Text + "’,’" + textBox3.Text + "’)");
cmd.ExecuteNonQuery();
connect.Close();
MessageBox.Show("Bilgiler Başarıyla Eklendi.", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button2_Click_1(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("Geçerli Kimlik Numarası Giriniz.", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
DialogResult a;
a = MessageBox.Show(textBox1.Text + " Kimlik Numaralı Kişiyi Silmek İstediğinizden Emin Misiniz.", "Bilgi", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (a == DialogResult.Yes)
{
connect.Open();
cmd.Connection = connect;
cmd.CommandText = ("Delete*From bilgiler Where tc_no=’" + textBox1.Text + "’");
cmd.ExecuteNonQuery();
connect.Close();
Temizle();
}
}
private void button4_Click_1(object sender, EventArgs e)
{
if (textBox1.Text == "Numara Seçiniz…")
{
MessageBox.Show("Öğrenci numarasını giriniz.", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
connect.Open();
cmd.Connection = connect;
cmd.CommandText = ("Select*From bilgiler Where tc_no=’" + textBox1.Text + "’");
read = cmd.ExecuteReader();
while (read.Read())
{
textBox1.Text = read["tc_no"].ToString();
textBox2.Text = read["adi"].ToString();
textBox3.Text = read["soyadi"].ToString();
}
read.Dispose();
connect.Close();
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("Aradığınız Kişi bulunamadı."
, "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
}
private void button3_Click_1(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("T.C Kimlik Numarası Giriniz.", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
connect.Open();
cmd.Connection = connect;
cmd.CommandText = ("UPDATE bilgiler SET tc_no=’" + textBox1.Text + "’,adi=’" + textBox2.Text + "’,soyadi=’" + textBox3.Text + "’Where tc_no=’" + textBox1.Text + "’");
cmd.ExecuteNonQuery();
connect.Close();
MessageBox.Show("Bilgiler Güncellendi.", "Bilgi", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
}
}[/code]