Nasıl Yapılır

C# İle Örnekler #39

c

C# Veritabanı Bağlantısı (İlk Kayıt, Önceki Kayıt, Sonraki Kayıt ,Son Kayıt)

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 vtilk_sonraki
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        SqlDataAdapter da;
        DataSet ds;
        int i = 0;
        SqlConnection conn;
 
        private void Form1_Load(object sender, EventArgs e) //Form Yüklendiğinde ilk kayıt
        {
            conn = new SqlConnection("server =.; Initial Catalog = okul; Integrated Security = SSPI");
            conn.Open();
            da = new SqlDataAdapter("select * from ogrenci", conn);
            ds = new DataSet();
            da.Fill(ds, "ogrenci");
 
            textBox1.Text = ds.Tables[0].Rows[i]["ogrenci_no"].ToString();
            textBox2.Text = ds.Tables[0].Rows[i]["ogrenci_ad"].ToString();
            textBox3.Text = ds.Tables[0].Rows[i]["ogrenci_soyad"].ToString();
            textBox4.Text = ds.Tables[0].Rows[i]["ogrenci_sehir"].ToString();
        }
 
        private void button3_Click(object sender, EventArgs e) //Sonraki Kayıt
        {
            if (i < ds.Tables[0].Rows.Count - 1) { i++; textBox1.Text = ds.Tables[0].Rows[i]["ogrenci_no"].ToString(); textBox2.Text = ds.Tables[0].Rows[i]["ogrenci_ad"].ToString(); textBox3.Text = ds.Tables[0].Rows[i]["ogrenci_soyad"].ToString(); textBox4.Text = ds.Tables[0].Rows[i]["ogrenci_sehir"].ToString(); } else { } } private void button2_Click(object sender, EventArgs e) //Önceki Kayıt { if (i == ds.Tables[0].Rows.Count - 1 || i != 0) { i--; textBox1.Text = ds.Tables[0].Rows[i]["ogrenci_no"].ToString(); textBox2.Text = ds.Tables[0].Rows[i]["ogrenci_ad"].ToString(); textBox3.Text = ds.Tables[0].Rows[i]["ogrenci_soyad"].ToString(); textBox4.Text = ds.Tables[0].Rows[i]["ogrenci_sehir"].ToString(); } else { } } private void button1_Click(object sender, EventArgs e)//İlk Kayıt { if (ds.Tables[0].Rows.Count > 0)
            {
                i = 0;
                textBox1.Text = ds.Tables[0].Rows[i]["ogrenci_no"].ToString();
                textBox2.Text = ds.Tables[0].Rows[i]["ogrenci_ad"].ToString();
                textBox3.Text = ds.Tables[0].Rows[i]["ogrenci_soyad"].ToString();
                textBox4.Text = ds.Tables[0].Rows[i]["ogrenci_sehir"].ToString();
            }
        }
 
        private void button4_Click(object sender, EventArgs e)//Son Kayıt
        {
            i = ds.Tables[0].Rows.Count - 1;
            textBox1.Text = ds.Tables[0].Rows[i]["ogrenci_no"].ToString();
            textBox2.Text = ds.Tables[0].Rows[i]["ogrenci_ad"].ToString();
            textBox3.Text = ds.Tables[0].Rows[i]["ogrenci_soyad"].ToString();
            textBox4.Text = ds.Tables[0].Rows[i]["ogrenci_sehir"].ToString();
        }
    }
}

 

Yazar Hakkında

Hız Hosting

Yorum Yap