C# ile SQL Server Uygulmaları(SQLConnection- SQLCommand-Dataset-SqlDataadapter)

C# ile SQL Server Uygulmaları(SQLConnection- SQLCommand-Dataset-SqlDataadapter) ile örneğe ulaşmak için tıklayınız.

Uygulama iki kısımdan oluşmaktadır. Uygulama tarafı bir de veritabanı kısmı. Örnek olduğu için veritabanı kısmında 1 adet tablo bulunmaktadır.

Veritabanında bulunan tablo aşağıda gösterilmektedir.

 

 

 

 

 

 

 

Uygulama ekranı ve kodlar aşağıdadır.

 

 

 

 

 

 

 

 

 

 

 

 

 

KOD BLOĞU:

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 ders_ornek
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
SqlConnection cnn=new SqlConnection(“Data Source=.\\SQLEXPRESS;Initial Catalog=ders-ornek; Integrated Security=true;”);
DataSet ds = new DataSet();
private void button1_Click(object sender, EventArgs e)
{
try
{
cnn.Open();
MessageBox.Show(“Bağlanti Başarılı… “);
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show(“Bağlantı açılamadı… “);
}
}

private void button2_Click(object sender, EventArgs e)
{
ds.Clear();
string sorgu = “Select * from bilgi”;
try
{
cnn.Open();
SqlDataAdapter adp = new SqlDataAdapter(sorgu,cnn);
adp.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
ds.Dispose();

cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show(“Bağlantı açılamadı… “);
}
}

private void button3_Click(object sender, EventArgs e)
{
ds.Clear();
string sorgu = “Select * from bilgi where Adi='”+textBox1.Text+”‘”;
try
{
cnn.Open();
SqlDataAdapter adp = new SqlDataAdapter(sorgu, cnn);
adp.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
ds.Dispose();
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show(“Bağlantı açılamadı… “);
}
}

private void button4_Click(object sender, EventArgs e)
{
string sorgu = “Select * from bilgi “;
try
{
cnn.Open();
cmd = new SqlCommand(sorgu, cnn);
reader = cmd.ExecuteReader();
while (reader.Read())
{
comboBox1.Items.Add(reader.GetValue(1));
comboBox2.Items.Add(reader.GetValue(2));
}
reader.Close();
cmd.Dispose();
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show(“Bir sorun oluştu. Tekrar deneyiniz…”);
}
}
}
}

 

 

admin