Mavzu: Ma’lumotlar bazasini yangilash. Reja


Download 5.39 Kb.
Sana19.06.2023
Hajmi5.39 Kb.
#1611712
Bog'liq
Mavzu Ma’lumotlar bazasini yangilash. Reja-hozir.org


Mavzu: Ma’lumotlar bazasini yangilash. Reja

Maqsad: Talabalar ma’lumotlar bazasini yangilash, MBga satr qo‘shish, satrlarni qidirish va o‘chirish haqida tasavvurga ega bo’lish.
MAVZU: Ma’lumotlar bazasini yangilash.

REJA:


  • Ma’lumotlar bazasini yangilash.

  • MBga satr qo‘shish.

  • Satrlarni qidirish va o‘chirish.

MBga satr qo‘shish


Biz biror bir jadvalga satr qo’shishimiz uchun yuqoridagi rasmda ko’rsatilgan obyektlardan foydalanamiz. Bu yerda, Label, textBox, button va DataGridView dan foydalanilgan.
“YUKLASH” nomli tugmaga ikki marta chertib, unga quyidagi buyruqlarni kiritamiz:
private void yuklash_Click(object sender, EventArgs e)
{
string connectionString = @"Data Source=.\SQLEXPRESS; Initial Catalog=Hodimlar; Integrated Security=True";
string sql = "SELECT * FROM Teachers";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlDataAdapter yuklash = new SqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
yuklash.Fill(ds);
DataTable dt = ds.Tables[0];
dataGridView1.DataSource = dt;
}
}
“QO’SHISH” nomli tugmaga ikki marta chertib, unga quyidagi buyruqlarni kiritamiz:
private void qushish_Click(object sender, EventArgs e)
{
string connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=Hodimlar;Integrated Security=True";
string sql = "SELECT * FROM Teachers";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlDataAdapter yuklash = new SqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
yuklash.Fill(ds);
DataTable dt = ds.Tables[0];
Davomi bor (keyingi slaydga qarang)!
“QO’SHISH” nomli tugmaga ikki marta chertib, unga quyidagi buyruqlarni kiritamiz(davomi):
// yangi qator qo'shish
DataRow newRow = dt.NewRow();
newRow["Ismi"] = textBox1.Text;
newRow["Familiyasi"] = textBox2.Text;
newRow["Fani"] = textBox3.Text;
dt.Rows.Add(newRow);
//SqlCommandBuilder obyektini hosil qilish
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(yuklash);
yuklash.Update(ds);
dataGridView1.DataSource = dt;
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
}
F5 funksional tugmasi yordamida dasturni ishga tushirib, natijani tekshirib ko’ramiz:
Form1 oynasining dastlabki ko’rinishi
Dastur natijasini tekshirish:
“YUKLASH” tugmasini tanlagamizdagi ko’rinish
Dastur natijasini tekshirish:
textBox larni ma’lumot bilan to’ldirib, “QO’SHISH” tugmasini tanlagamizdagi ko’rinish
Natijani SQL Server Management Studio dagi ma’lumotlar bazasidagi ko’rinishi:
Ma’lumotlar bazasidan qidirish
Biz ma’lumot qidirishimiz uchun yuqoridagi rasmda ko’rsatilgan obyektlardan foydalanamiz. Bu yerda, Label, textBox va DataGridView dan foydalanilgan.
public partial class Form1 : Form
{
string ulash = @"Data Source=.\SQLEXPRESS; Initial Catalog=dorixona; Integrated Security=True";
string yuklash = "SELECT * FROM dorilar";
DataSet ds;
DataTable dt = new DataTable("dorilar");
public Form1()
{
InitializeComponent();
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.AllowUserToAddRows = true;
}
Ma’lumotlar bazasining asosiy o’zgaruvchilarini e’lon qilish:
private void Form1_Load(object sender, EventArgs e)
{
using(SqlConnection con = new SqlConnection(ulash) )
{
SqlDataAdapter adap = new SqlDataAdapter(yuklash, con);
ds = new DataSet();
adap.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Columns["Id"].ReadOnly = true;
}
}
Form1_Load ni ishga tushirib, unga quyidagi dasturni kiritamiz:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
DataView dv = dt.DefaultView;
dv.RowFilter = string.Format("Dori_nomi LIKE '%{0}%'", textBox1.Text);
dataGridView1.DataSource = dv.ToTable();
}
}
textBox1 obyektini ishga tushirib, ma’lumot qidirish uchun unga quyidagi dasturni kiritamiz:
Bu yerda textBox1 obyektining KeyPress hodisasidan foydalanilgan. (Qarang: Properties oynaning Events bo’limiga).
Ma’lumotlar bazasidagi jadvaldan qatorlarni o’chirish
Bu yerda Formamizga “O’chirish” nomli yangi Button qo’shamiz.
private void button1_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
dataGridView1.Rows.Remove(row);
}
}
Button ga quyidagi kodni kiritsak, javaldan qatorlarni o’chirish imkoniyatiga ga bo’lamiz.

ETIBORINGGIZ UCHUN RAHMAT!



http://hozir.org
Download 5.39 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling