Showing posts with label connection. Show all posts
Showing posts with label connection. Show all posts

21 March 2010

Tengah berfikir..

Assalamulaikum wbt..




Click to enlarge


Gambar atas nie adalah workflow utk socket y aku prepare utk grid computing..pening mmg pening..jadi mmg jd..cume aku xsure workable or x bile combine dgn part Acad..hmm..

Sperti y ditunjukkan pd gambarajah di atas, aku write based on basic socket for client server. 3 hari gak berfikir smbil tgk CSI Las Vegas nih..Nseb bek la aku amik CST234 dulu, network programming.Kalu x, menjajah jgak nk tnye uncle googlerudin..

Aku rse cm complicated nk explain..cume pe y bleh di explain adalah sumer y kt atas tuh run on cross platform (windows + linux) and y pling penting, stiap server bleh jd client n vice versa..Alternating each other whenever needed.



i'm still thinking...








.

02 February 2010

Optimization VS Accuracy

Assalamulaikum wbt..

Ok come back to my world - programming. Seperti tajuk kat atas tu, hri nie aku nk cite sikit sal pendapat aku, ape y aku rase bagus n ape y aku rase xbgus utk kite selaku programmer buat especially those who are C# beginner like me.

"OPTIMIZATION & EFFICIENCY"

Sedar atau x, setiap line of programming of code y kite buat sebenarnye mempengaruhi kadar efficiency program kite as a whole. Aku bagi contoh utk buat 1 application, programmer A memerlukan 1000 line utk siapkan bende tue tp pd ms y sama, programmer B hanya memerlukan 300 lines jer. Jadi y mana y lebih efficient?? Pada aku, efficiency sesuatu programming code tue sebenanye terletak pd faktor turn around time dan memory consumption. Memang jelas kalu compare based on number of lines, programmer B mmg power, tp kalu application die buat laptop aku jd slow nk mati, xguna jugak kan. Kesimpulannya, sbb tue la kite semua selaku pelajar CS wajib amik CPT212 - Algorithm n Data Structure. Memang aku akui, aku mmg kagum gile dgn org y bleh develop sesuatu algorithm nie. Xtaw la baper byk sel2 neuron y berhubung dlm kepala die. So kenapa turn around time n memory consumption??

"TURN AROUND TIME"

Apekah die turn around time?? Aku bg 1 contoh y plig simple. Bile kite bukak page Yahoo n register akaun emel baru, brapa second y die amik utk mksure registeration kite succesful?? Kalu kite wat Transact-SQL n select some table from MS Sql, brape lama ms y diamik utk load all data kat interface?? Sumer nye berkaitan dengan masa kan..Pd pendapat aku, one thing y every programmer should think about is how long it should take to do the specified task. Aku penah baca buku Human & Computer Interaction (HCI) oleh Stalling kepunyaan Umi Mahmudah Mohd Jaafar (kakak aku) y kini sorang network traffic consultant for streamyx asia pacific. Dalam buku tue ade sebut sal 1 prinsip paling utama dlm design GUI - Respond must be given within 5-7 seconds. Dgn kata laen, lebih dri tue, user akan rase bengang sama mcm korang cbe connect USMHotspot xdpt2.

Aku bg 1 contoh implementation y mungkin akan bg serba sikit idea pasal reducing turn around time nih. Usually, ms pling byk dibazirkan dlm sesuatu application adalah utk load data or set data dlm database..Kene plak network slow, mmg cm taik ar. Setiap programmer ader cra msing2.. Tp kat sini nk bg contoh cra aku utk memudahkan pemahaman.. Besenye aku akn wat system aku xkire la web or desktop based utk pre-loading dlu sumer data2 y diperlukan n fill in dlm 1 container cam linklist, dataset or data table. Contohnye kalu application tue sal hotel management, aku akn load awal2 sumer info sal customer,registration and soon dlm 1 dataset or text file so that next time aku dh xperlu lgi access database. Hanya bile berlaku update or delete je bru aku access database n fetch blik new data. So, xderla user tue tekan 1 button, then system access database, tekan button lagi, access lagi, tekan then access lgi.Brapa byk nk access daa. Load jer sumer data awal2, store lam temporary container cm dataset, then bile nk access, access jer dri dataset tuh. Advantages cara nie ade 2 iaitu reduce number of connection y perlu kite wat (user toksah tunggu lama2) n application still works even internet connection gone.Best kan..How to do it? byk cra die, depends on what your task needs..

"MEMORY CONSUMPTION"

Haa part nie y aku plig suke nie.Penah x kalu korang wat website or desktop application then bukak task manager tgk braper byk memory consumption application korang?? Mesti xpenah kan, sbb y kite taw ialah janji siap n dpt submit on time. Actually bnde nie penting gak sebenarnye sbb not every user or customer y akn gune application kite nanti ade 2 or 3 gig RAM n Dual Core. Kite selaku programmer, kite kene la consider dorang jgak kan. Dorang pn manusia jgak mcm kite..Sbg contoh, software iNetmon utk dak2 network CS bleh capai 250++ KB total memory consumption n membuatkan laptop aku kdag2 jd slow especially bile turn on sumer module2 die. As a comparison, Office 2010 sometimes hanye dalam 70 - 100 KB jaa n kalu korang tgk, application dorang smooth jer. Senang hati nk menaip wat assignment kan..Dengan kate laen, kalu la korang bleh reduce memory consumption nie ke tahap membanggakan, insyaAllah application korang mesti super duper laju.

Aku bgi contoh la supaya korang mudah paham. Besenye kite declare variable kan. Ntah mane2 kite declare. Asal nk gune jer declare baru. Sedar or x, setiap declaration y kite wat akan menambahkan penggunaan memory. Camni, bile kite declare, system akn sediakan 1 memory address space utk variable tuh n lock utk kegunaan application kite shj. Once kite dh hbis gune, memory address tue still there n lock kecuali kalu kite dispose or null kan variable tue. Cemana aku nk explain erk. Let say la korang ade 1000 lines of programming code. Declare ar 1 global variable type string n gune jer variable tue as a temporary to store anything through out whole programm whenever needed. Jimat memory space sikit. 1 lagi adalah penggunaan List dan String. Cube compare declaration kat bwh nie:

string[] TempString = new string[100];
list TempList = new list();

Bile declare string, kite kene specify size die tul x? Belum tentu kite akn gune sumernye sampai habis. Bile declare jer string array, system dh sediakan n lock 100 memory space utk kite. membazir namanye tu..Xelok membazir ni, kwn syaitan..hehe.Solution nye, gune la List sbb list nie aku bleh kata cemana erk, ermm "memory on demand" kot.Sbb bile kite declare, xperlu specify size. Hanya bile add item dlm list tue je bru la system akn specifykan 1 memory address. Lagi efficient kan?

Contoh laen adalah penggunaan "using" statement dlm Object declaration.Aku bgi contoh code kat bwah nie:

Contoh 1:

void ReadFile()
{
StreamReader ObjReader = new StreamReader(@/"daunkelapasawit.txt");
String temp = ObjReader.Readline();
}

Contoh 2:

void ReadFile()
{
using ( StreamReader ObjReader = new StreamReader(@/"daunkelapasawit.txt"))
{
String temp = ObjReader.Readline();
}
}

Sori ar indentation xbtul, almaklum la write trus kat blog jer, mane ader intelliSense. So ape beza kedua2 contoh kat atas nie? Contoh 1 ada usual object declaration y bese kite buat. Bile kite exit function ReadFile, memory address utk Object tue actually still exist sebenanye. Makin byk bnde2 cmni, mkin berat la application kite. One of the solution kalu dlm C# adalah melalui penggunaan "using" statement. Maksudnye, systeam (or .Net Framework) akn detect bile Object tue dh x digunakan lgi (or out of the function scope), die akn automatically dispose Object tersebut.Dgn kate laen, bile dh xgune, buang jer la..Wat beratkan beg jer nk angkut ke sana ke mari.

Byk lagi cra2 utk makesure program kite running dengan smooth n steady. Nie antara cara2 y bleh kite guna selaku programmer. Ape pun, explore, try n gain knowledge by yourself is better than relying on someone..Gud luck!







.

26 August 2009

C# and database part 3

Assalamulaikum wbt..



entry y lepas aku dh explain line by line cmane bentuk C# bile wat database connection..itu hanya sedikit shj dri keseluruhan tutorial nie..kalu korang perasan, code y aku tnjukkan hanya utk RETRIEVE dri database sahaja..ofkos menggunakan "Select" statement..xda perubahan kat database..so..skang, aku nk tunjuk cmana nk execute command y melibatkan sebarang perubahan kepada database..

ok..before aku go any further..ade 3 jenis execute command y kite bleh gune bile deal ngn database iaitu ExecuteNonQuery,ExecuteScalar and ExecuteReader..ape beze antara ketiga2 execute command ni? untuk lebih memudahkan fahaman ajaran ni, aku explain dri y plig kecik ke plig besar

ExecuteReader digunakan utk read or retrieve dri databse..nama pun reader kn..kite xleh wat insert, update or delete statement gune command nie..hanya utk select statement sahaja..ape y istimewanya command nie adalah ia nya less burden pd databse kite utk handle query..juz read byte of stream and pass it back..

Next, ExecuteScalar..command nie hanya akan return 1 value shj..igt!..1 value sahaja..aku jarang gune ExecuteReader actually, most of the time aku akn gune ExecuteScalar nie..kenapa nk return 1 value? best gak soklan tue..ok..situasi nye cmni..letsay korang ader byk record dlm database ( beratus kot)..n then korang nk retrieve braper total record y dh ade dlm database..haa kat sini la kite bleh gune ExecuteScalar..gune select count statement, then pass kat ExecuteScalar..die akn return balik 1 value..iaitu total record y kite ader..cmtu la kegunaan nya..ia bkn hanya utk kire total record..tp apa2 SQL statement y hanya akn return 1 value jer..menjimatkan ms+stamina databse kite utk handle..

Next please...k now ExecuteNonQuery..nie option y plig las y kite ade bile nk execute command to databse..command nie khas utk SQL statement y melibatkan perubahan kepada database cm INSERT,UPDATE,DELETE and soon la..ape y kite wat ialah pass SQL statement kite kepada ExecuteNonQuery n bia die setelkan..then korang tgk databse, mesti dh insert..

Next entry akn aku tnjukkan how to write the code n hopefully korang leh follow lah yerk..Stakat nie.korang faham y nie jer dlu k..jgn dok pening2..haha

24 August 2009

C# and database part 2

Assalamulaikum wbt..

entry lepas aku da post sal cmane nk gne C# utk connect to any database..entry kali nie utk aku terangan line by line programming code y crucial pd sesape y nk follow..mksure korang dh bace entry sebelom nie utk prevent otak kite dri terlalu sasau..

before aku pg lebih jauh..nk terang ckit sal database connection. Da 2 mode utk connect to database (xkire apa language pn y korang gune)..Pertama, always connect n y kedua, temporary connect..Option nie depend on what software architecture y kite impliment. "Always connect" memerlukan apps kte utk open connection to database (n hold dat particular connection) from start up till user tekan exit button..keep in mind, one enterprise database server can only hold up to 24 simultaneous connection (kalu personal lgi la kurang)..suggestion aku, gune option kedua iaitu "temporary connect"..means apps xperlukan databse connection utk run from startup till da end, tp hanya open connection bile perlukan access ke databse shj, then close blik connection..itu shj..cara ni lagi menjimatkn dn mengelakkan server dri overload utk handle sumer databse request..cube bayangkan kalu server syok.org gune option y pertama, maka xdpt la kite nk download citer District 9 mlm nie..ekekeke..so, explaination y seterusnya hanya fokus kepada option y kedua td ok..(leh follow x ni?)

MS SQL Server version:

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

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

SqlConnection conn = new SqlConnection(connectionString); //line 3
Conn.Open(); //line 4

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

DataSet ds = new DataSet; //line 8
da.Fill(ds); //line 9

conn.Close(); //line 10
}


line 1 -- letak referrence kpd database client y kite nk gne. depend what type of databse ykite gune.

line 2 -- initialize 1 connection string, letak la username n password sumer tue..string nie mmg da semicolon y byk, coz string nie akn kite pass kat databse client td utk die split2 kn blik n casting them to respective variables.

line 3 -- initialize 1 database connection handler (in my case, conn)..mksure letak connction string dlm parameter connection handler nie td..kalu x, die xtaw nk gne connection ape..

line 4 -- gne connection handler td, open connection to database.

line 5 -- cm bese, letak la query ape y korag nk wat dlm bntuk 1 string..xksah la pnjag mane, tp mksure sql query tue btul syntax nye.

line 6 -- initialize 1 comand handler. Fungsi die adalah sebagai tukang "execute"kan query kite td. Tanpa die, sapa la query kite.Dlm parameter die, mksure letak skali string query y kite nk gune, n connection handler y die nk pakai..then die akn execute query tue thru connection y kiter dh open td (conn.Open())

line 7 -- Pe fungsi data adapter nie? fungsi die adalah sebagai orag tengah utk simpan data y kite execute td secara temporary..mksure parameter y digunakan exactly same as command handler td..

line 8 -- initialize 1 data structure or container utk receive data tue td..data structure may be vary, korag leh gune string, array, variable, data table, dataset and soon..in my case, data set..

line 9 -- pgil org tengah y hold data temporary td ( data adapter )...suh die "fill" data y die receive dri database tue ke dlm data structure kite td iaitu data set..

line 10 -- lastly, don poget to close da connection..

itu la sahaja penerangan die..pening? itulah die the best part about programming..hahaha

da soklan? bek tanye..rugi x tanye