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

C# နဲ ့ sql server အသုံးပြုပြီးတော့
* database ထဲကုုိ data တွေ ဘယ်လိုပို ့မလဲ
* database ထဲက data တွေကို form ပေါ်မှာ ဘယ်လိုပြမလဲ
* database ထဲက data တွေကို ဘယ်လို update လုပ်မလဲ
* database ထဲက data တွေကို ဘယ်လို delete လုပ်မလဲ
* BindingSource ကို ဘာအတွက် အသုံးပြုမှာလဲ
အဲဒါတွေကို အပိုင်းလိုက် ခွဲပြီးတော့ ရှင်းပြပါမယ်။

Software Requirement အနေနဲ ့ Microsoft Visual Studio တင်ထားရပါမယ်။ ကျနော် သုံးနေတဲ့ Microsoft Visual Studio 2008 Team System မှာ Microsoft SQL Server 2005 Database Engine ပါပါတယ်။ ဒါပေမဲ့ သူချည်းပဲ မရပါဘူး။ SSMSEE (SQL Server Management Studio Express Edition) လိုပါသေးတယ်။ SSMSEE လိုချင်ရင် ဒီမှာ ဒေါင်းပါ။

ပထမဆုံး SQL Management Studio ဖွင့်ပြီးတော့ database တစ်ခု ဆောက်ပါမယ်။
SQL Server Management Studio -> Object Explorer -> Server Node -> Database (right click) -> New Database
Figure1_1 မှာ ကြည့်ပါ။


Figure1_1

name ကို SQLSAMPLE ဆိုပြီး ပေးလိုက်ပါတယ်။ Figure1_2 မှာ ကြည့်ပါ။ Databases ထဲမှာ SQLSAMPLE ဆိုတဲ့ database ၀င်သွားတာ တွေ ့ပါလိမ့်မယ်။


Figure1_2

New Query ခေါ်ပြီး database table တစ်ခု ဆောက်ပါမယ်။

USE SQLSAMPLE
GO

CREATE TABLE SAMPLE (
ID INT IDENTITY(1,1) NOT NULL,
FIRSTNAME VARCHAR(100),
LASTNAME VARCHAR(100))
GO

F5 (or) Execute န ဲ့ ကြည့်ကြည့်ပါ။  Figure1_3 အတိုင်း မြင်ရပါလိမ့်မယ်။


Figure1_3

Sql Command သုံးပါပြီ။ table ထဲမှာ ရှိသမျှအားလုံးကို SELECT Statement နဲ ့ ဆွဲထုတ်ပါပြီ။ ပြီးရင် Execute လုပ်ပါမယ်။

Figure1_4 မှာကြည့်ပါ။ အောက်မှာ Table ထွက်လာပါပြီ။


Figure1_4

Sql Server မှာ database ဆောက်ပြီးပြီဆိုတော့ C# အပိုင်း ကြည့်ရအောင်။ Visual Studio ကို New Project ခေါ်ပြီး Windows Forms Applications ရွေးလိုက်။ ပြီးရင် အောက်ကပုံအတိုင်း


Figure1_5

[COLOR=”DeepSkyBlue”][U]Properties[/U][/COLOR]
form ထဲကို Label (၂)ခု၊ textBox (၂)ခု၊ Button (၁)ခု နဲ ့ DataGridView (၁)ခု ထည့်ပါမယ်။
Label1 ရဲ ့ Text ကို First Name
Label2 ရဲ ့ Text ကို Last Name
textBox1 ရဲ ့ Name property ကို txtFirstName
textBox2 ရဲ ့ Name property ကို txtLastName
button1 ရဲဲ ့ Name property ကို btnAddRecord, Text Propery ကို Add Record
စသည်ဖြင့် ပြင်လိုက်။

ပြီးရင် Add Record Button ကို double click ပေးပြီး သူ ့ရဲ ့အလုပ်လုပ်ပုံကို ရေးရအောင်။
ပထမဆုံး SqlConnection class နဲ ့ SQL Server ကို လှမ်းချိတ်မယ်။

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

MRDREAM ဆိုတာ ကျနော့် Computer Name ပါ။ ခင်ဗျားကတော့ ခင်ဗျား Computer Name ကို သုံးပေါ့။ SQLEXPRESS ဆိုတာ SQL Server ကို ပြောတာ။ SQLSAMPLE က ကျနော်တို ့ ဆွဲခဲ့တဲ့ database name ကို ပြောတာ။ SSPI(Security Support Provider Interface)  ဆိုတာ Microsoft Windows System တွေအတွက် authentication လိုမျိုး ကိစ္စတွေမှာ security အတွက် ရေးထားတဲ့ API တစ်ခုပါ။ သူ ့ရဲ ့ dll (direct-link-library) က security နဲ ့ ပါတ်သတ်တဲ့ packages တွေ လုပ်ပေးတယ်။ ဆိုလိုတာက လက်ရှိ program နဲ ့ database အတွက် security လုပ်ထားတယ်ဆိုတဲ့ သဘောမျိုးပေါ့။

SqlDataAdapter da = new SqlDataAdapter();

DataAdapter တစ်ခုဆောက်လိုက်ပါပြီ။ ကျနော်တို ့လုပ်ချင်တာက TextBox ကနေ First Name နဲ ့ Last Name ကို ရိုက်ထည့်လိုက်မယ်ဆိုရင် sql server ထဲမှာ အဲ data ၀င်သွားရမယ်။ ဆိုတော့ SqlCommand class ထဲက InsertCommand property ကို အသုံးပြုမယ်။ ဒီ InsertCommand property ကို records အသစ်တွေ database ထဲ ထည့်မယ်ဆို အသုံးပြုပါ။

da.InsertCommand = new SqlCommand(“Insert Into SAMPLE values(@FirstName, @LastName)”, conn);

@FirstName နဲ ့@LastName ဆိုတာ SAMPLE Table ထဲက parameter နှစ်ခု။ Table အနေနဲ ့ ပြောရမယ်ဆိုရင် Column Name တွေပေါ့။ ဟုတ်ပြီနော်။ ဒီတော့ အဲဒီ့ parameters တွေကို ကျနော်တို ့ ဘာလုပ်ရမလဲဆိုတော့

da.InsertCommand.Parameters.Add(“@FirstName”, SqlDbType.VarChar).Value = txtFirstName.Text;
da.InsertCommand.Parameters.Add(“@LastName”, SqlDbType.VarChar).Value = txtLastName.Text;

InsertCommand နဲ ့ Parameters ထဲကို FirstName နဲ ့LastName parameters တွေကို Add လုပ်မယ်။ အဲမှာ ကျနော်တို ့ SQL Management Studio မှာ SAMPLE ဆိုတဲ့ database table ဆွဲတာကို ပြန် သတိရကြည့်ပါ။

FIRSTNAME VARCHAR(100),
LASTNAME VARCHAR(100)

ဟုတ်ပြီနော် .. FirstName ကော LastName ကော Sql Data Type အနေနဲ ့ VARCHAR ပေးခဲ့တယ်နော်။
ဟုတ်ပြီ။ ဒါကြောင့် SqlDbType.VarChar ဖြစ်နေတာ။ ပေါက်တယ်နော်။ နောက်ဆုံး အဲဒီ့ parameter ရဲ ့ Value ကို ကျနော်တို ့ First Name နဲ့ Last Name ရိုက်ထည့်မဲ့ textBox ထဲက Text ကို ထည့်ပေးမယ်။

အိုကေ အဲဒါဆို နောက် ကျန်တာလေး ကြည့်ရအောင်။

conn.Open();
da.InsertCommand.ExecuteNonQuery();
conn.Close();

Connection ဖွင့်မယ်။ ExecuteNonQuery() method နဲ ့ record တွေကို update လုပ်မယ်။ အရင် post မှာကတည်းက ExecuteNonQuery() အကြောင်း ပြောပြီးသားပါ။ ပြီးရင် connection ပိတ်မယ်။ အိုကေ ။ ကဲ result ကြည့်ရအောင်။ ခုရေးခဲ့တဲ့ Add Record Button ရဲ ့ code ပါ။

private void btnAddRecord_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(“Data Source=MRDREAM\\SQLEXPRESS;” +
“Initial Catalog=SQLSAMPLE; Integrated Security=SSPI”);
SqlDataAdapter da = new SqlDataAdapter();
da.InsertCommand = new SqlCommand(“Insert Into SAMPLE values(@FirstName, @LastName)”, conn);
da.InsertCommand.Parameters.Add(“@FirstName”, SqlDbType.VarChar).Value = txtFirstName.Text;
da.InsertCommand.Parameters.Add(“@LastName”, SqlDbType.VarChar).Value = txtLastName.Text;

conn.Open();
da.InsertCommand.ExecuteNonQuery();
conn.Close();
}

 

Output


Figure1_6

Figure1_6 မှာ ကျနော် First Name ကို Scarlett ၊  Last Name ကို Johansson ထည့်လိုက်တယ်။ ဒါပေမဲ့ DataGridView နဲ ့မချိတ်ထားတဲ့အတွက် မပြပါဘူး။ ဒီအတွက် ကျနော်တို ့ SQL Management Studio မှာ တစ်ချက် သွားကြည့်ရအောင်။ Figure1_7 မှာ ကြည့်ပါ။


င်္Figure1_7
SELECT * FROM SAMPLE

ကို click လုပ်ပြီး Execute နဲ ့ run လိုက်ပါ။ ပုံမှာ မြင်ရတဲ့အတိုင်း ပါပဲ။ အဲဒီ့မှာ ID ကို တစ်ချက် သတိထားကြည့်ပါ။ နံပါတ် (၄) ဖြစ်နေတယ်။ ကျနော်က First Name Last Name သုံးကြိမ် ထည့်ပြီး ပြန်ဖျက်ထားလို ့အခုက ID no 4 ဖြစ်နေတာပါ။ ဒီထဲမှာ ID နဲ ့ ပါတ်သတ်လို ့ ဘာမှ မလုပ်ထားတဲ့အတွက် SQL Management Studio က သူ ့ဘာသာ ID No. ကို duplicate မဖြစ်အောင် ရေးပေးသွားတာပါ။  ကျနော် နောက်ထပ် Jessica Alba နဲ ့ Eun Yoon Hye ကို ထပ်ထည့်လိုက်ပါတယ်။ Figure1_8 မှာကြည့်ပါ။


Figure1_8

နောက်ထပ် တစ်ဆင့်ချင်း ဆက်သွားပါမယ်။ part 2 မှာ စောင့်ကြည့်ပါ။
ခု ကျနော်ရေးထားတာကို သဘောကျရင် youtube က babliobr ကို သွားပြီး rating ပေးလိုက်ပါအုံး။

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

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

  1. ဒီလို ပို့စ်တွေဖတ်ခွင့်ရတာ အရမ်းကျေးဇူးတင်တယ်ပဲ ပြောရမှာပဲ….. ခုမှသိလို့ နည်းနည်း စိတ်မကောင်းဖြစ်မိတယ် … တကယ်ဆို လွန်ခဲ့တဲ့ ရလ ၈လ လောက်တည်းက သိခဲ့သင့်တာ……..

  2. အကိုရေ ကျွန်တော် ဒီ အောက်က command လေး ရိုက်တာက စပြီးတော့ error စတက်နေတာပဲ အကို .. အကိုပြောသလို ကျွန်တော့ ကွန်ပျူတာ အမည်ကိုလည်း ပြောင်းပါတယ် … ဒါပေမယ့် C# မှာ SqlConnection ဆိုတာက စပြီးတော့ Error စတွေ့တာပဲ ..

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

    ကျွန်တော် သုံးနေတာက SQL Server 2008 နဲ့ Virtual Studio 2010 ပါ … ကျွန်တော် မှားနေတာလေးကို ပြန်ရှင်းပြပေးပါနော် ..

    1. မေးတဲ့ အခါမှာ ဘာ error message ပြလဲဆိုတာပါရင်ကောင်းမယ်။ ပြီးတော့ http://www.mysteryzillion.org မှာ သွားမေးရင် အဖြေ မြန်မြန် ရမယ်ထင်ပါတယ်။

Leave a Reply

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