20 August 2009

C# and database sending and receiving

Assalamulaikum wbt..

Aaa akhir nya da la orang tanye soklan2 programming nie..dh lame aku tunggu2 soklan2 y mencabar minda cmni..tahnah kepada amar kerana memberi aku nafas baru dlm bidang penulisan blog y penuh dengan mehnah dan tribulasi ini..hehe..

Ok, seperti mauduk y tertera di atas, aku akn terangkn serba sedikit tentang cemana nk connect ke database secara programmatically thru C#..xksah ape platform, either windows form application, ASP.NET, WPF or silverlight, sumer same jerr..tp da sdikit perubahan bile integrate ngn WCF technology (xper, nanti aku terangkn kalu sempat)..coz WCF adalah 1 platform len y focus utk data sending or receiving xkire ke database, server or mobile phone..tp sume platform2 ni adalah utk .Net Framework 2.0 dan ke atas sahaja..dun wory, basically majority semua VS related SDK skg dh compliment ngn framework nie.

first, kite kne tw ape database y kite nk gune. Bleh gne MS SQL Server, MySQL, Oracle, Access n len2..bnde ni amat penting utk kite tw ape function y kite nk gne nanti..Regardless ape jenis database pn y korang gne, setiap connection thru database mesti gne basic structure or step y akn aku terangkn nanti.Yang bezanya hanya nama function jer..hehe

basic step utk connect database:

1 - add system referrence mengikut database ape y kite gne
2 - initialize 1 connection string
3 - initialize database connection handler
4 - passing connection string kepad aconnection handler as a parameter.
5 - Open connection
6 - initialize database command handler
7 - wat query dlm bntuk string
8 - passing string query + connection handler td kepada command handler as a parameter.
9 - execute query
10 - close connection

thats all..senang kn..hehe

skg aku tnjuk 2 contoh dri 2 database berbeze, MS SQL Server n MySQL cemana nk RETRIEVE data dri database.

MS SQL Server version:

using System.Data.SqlClient
.
......(smthing constructor kat sini)........
.
.
private void retrieveData()
{

string connectionString = "Server=myServerAddress; Database=myDataBase;UserID=myUsername; Password=myPassword;";

SqlConnection conn = new SqlConnection(connectionString);
Conn.Open();

string selectStatement = "select something from someTable where someID = 1":
SqlCommand command = new SqlCommand(selectStatement, conn);
SqlDataAdapter da = new SqlDataAdapter(selectStatement,conn);

DataSet ds = new DataSet;
da.Fill(ds);

conn.Close();
}

MySQL version:

using MySql.Client;
.
......(smthing constructor kat sini)........
.
.
private void retrieveData()
{

string connectionString = "Server=myServerAddress; Database=myDataBase;UserID=myUsername; Password=myPassword;";

MySqlConnection conn = new MySqlConnection(connectionString);
Conn.Open();

string selectStatement = "select something from someTable where someID = 1":
MySqlCommand command = new MySqlCommand(selectStatement, conn);
MySqlDataAdapter da = new MySqlDataAdapter(selectStatement,conn);

DataSet ds = new DataSet;
da.Fill(ds);

conn.Close();
}

Camtu la ceritanya utk basc retrieving dri database. Kalu korang perasan, antara dua contoh y aku tnjuk ni, xder beza pn, step still sama, cume beza hanya pd nama function shj..perati btul2 k..wat ms nie, tgk y nie jer dlu..bagi intro jer ckit..next post, aku akn terangkn line by line code y aku tulih nie..supaya memudahkan korang faham+develop foudation dlu.

k, any soklan, sila2 la tanye..

3 comments:

  1. wah...
    menarik..
    kena try nih...
    thanks..

    y kat
    UserID=myUsername; Password=myPassword;";

    memang ada "; ke???
    or tersilap taip..??

    ReplyDelete
  2. mmg ada ";" dlm connection string..

    connection string ini basically adalah 1 string..means, kite pass sume variable (username, password,database name etc) as a 1 long string..Once database receive string pnjang nih, die akn analyze+split2 based on ";"..bile dh split2..then baru database compare priviledges..

    agak2 menjawab soklan x?

    ReplyDelete
  3. ooo..
    baru perasan y semicolon tuh bersambung dari atas..

    kira cam kita kasik command masuk dalam string pastu paskan utk establish connection la kan..

    hmm...
    ok2..

    ReplyDelete

terima kasih.