Sql Server, C# and ADO.NET(part 2)

ပြီးခဲ့တဲ့ part 1 မှာ Sql server ထဲကို data ထည့်သွင်းခြင်းကို ရှင်းပြခဲ့ပြီး ဖြစ်ပါတယ်… ဒီအပိုင်းမှာ အဲဒီ့ database ကို  form ပေါ်မှာ မြင်နိုင်အောင် DataGridView သုံးပြီးတော့ display လုပ်ပါမယ်.. ကဲ ကြည့်ရအောင်..

ကျနော်တို ့part 1 မှာတုန်းက Form ပေါ်မှာ Figure1_1 မှာ ပြထားသလို Sql Server ထဲကို data တွေ ထည့်သွင်းထားတယ် ဆိုပါစို ့..

အဲဒါကို Form ပေါ်မှာ display ဘယ်လိုလုပ်မလဲဆိုတာ ကြည့်ပါ… ပထမဆုံး Figure1_2 မှာ ပြထားတဲ့အတိုင်း Form ပေါ်မှာ Button အသစ်တစ်ခု ထပ်ထည့်လိုက်ပါအုံး..


Figure1_2

button’s property အနေနဲ ့ Name ကို btnDisplay နဲ ့ Text မှာ Display လို ့ပြင်ရေးလိုက်ပါ…
ပြီးရင် button ကို double click ပေးပြီး သူ ့အလုပ်လုပ်ပုံကို ရေးရအောင်..

SqlConnection conn = new SqlConnection(“Data Source=MRDREAM\\SQLEXPRESS;” +
“Initial Catalog=SQLSAMPLE; Integrated Security=SSPI”);
SqlDataAdapter da = new SqlDataAdapter();

ပထမဆုံး connection ချိတ်မယ်.. ပြီးရင် dataAdapter ကို create လုပ်မယ်.. part 1 က Add Record button အလုပ်လုပ်ပုံမှာ ရှင်းပြပြီးသားဖြစ်တဲ့အတွက် ထပ် မရှင်းတော့ပါဘူး..

DataSet ds = new DataSet();

ADO.NET မှာ data source ထဲက data တွေဟာ dataAdapter အကူအညီနဲ ့ DataSet ထဲကို ရောက်ပါတယ်။ dataSet ထဲက data တွေကိုမှ Form Controls တွေပေါ် data binding လုပ်လိုက်တာပါ..
ဒါကြောင့် အခု DataSet ကို create လုပ်လိုက်ပါတယ်..

da.SelectCommand = new SqlCommand(“SELECT * FROM SAMPLE”, conn);

part 1 မှာတုန်းက sql server ထဲက database ထဲကို data တွေ ထည့်သွင်းမှာဖြစ်တဲ့အတွက်
dataAdapter.InsertCommand ကို အသုံးပြုခဲ့တာ မှတ်မိမှာပါ.. အခုက database ထဲက ဟာကို Form Controls ပေါ် တင်မှာဖြစ်တဲ့အတွက် dataAdapter.SelectCommand နဲ ့ အသုံးပြုပါမယ်။
SELECT * FROM SAMPLE  မှာ SAMPLE က database ရဲ ့  Table name၊ * ဆိုတာ Table ထဲမှာ ရှိသမျှ data အားလုံး…

ds.Clear();

DataSet ကို clear လုပ်ပေးတယ်ဆိုတော့ ဘာအတွက်လဲ မေးစရာ ရှိပါတယ်.. သူ ့ကို clear ပေးမထားရင် data အသစ် တစ်ခု ထည့်လိုက်တာနဲ ့ အရင် data တွေပါ ပြန်ပြီး display ထပ်လုပ်ပါလိမ့််မယ်.. အဲလိုမဖြစ်ရအောင်လို ့ကာကွယ်ထားတာပါ.. မသိသေးသူများ ရှေ ့မှာ comment လေးပိတ်ပြီး run ကြည့်ပါ…

da.Fill(ds);

dataAdapter နဲ ့ DataSet ထဲကို data တွေ Fill ဖြည့်လိုက်တာပါ..

dataGridView1.DataSource = ds.Tables[0];

နောက်ဆုံး DataGridView ရဲ ့ DataSource ထဲကို DataSet ထဲက dataTables တွေကို ထည့်လိုက်ခြင်းအားဖြင့် Form ရဲ ့ DataGridView ပေါ်မှာ မြင်ရပါလိမ့်မယ်..

ခုရေးခဲ့တဲ့ Display button ရဲ ့ code ပါ။

private void btnDisplay_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(“Data Source=MRDREAM\\SQLEXPRESS;” +
“Initial Catalog=SQLSAMPLE; Integrated Security=SSPI”);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();

da.SelectCommand = new SqlCommand(“SELECT * FROM SAMPLE”, conn);
ds.Clear();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}

Output


Figure1_3

ကျနော်တို ့ data အသစ်လေးတွေ ထပ်ထည့်ကြည့်မယ်.. Figure1_4 မှာ ကြည့်ပါ…


Figure1_4

ဆက်ရန် (part 3)

ok!
Thanks for reading!!
See u around, buddy!!!

CategoriesC#

2 Replies to “Sql Server, C# and ADO.NET(part 2)”

Leave a Reply to thazin Cancel reply

Your email address will not be published. Required fields are marked *