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
24 August 2009
Subscribe to:
Post Comments (Atom)
hmm..
ReplyDeletenak tanye pe ek..
hmm..
nmpak cam senang..
kena try..
kat sume db sama ke??
access,sql server,oracle??
hmm...
utk connect ms sql..
kena create user ke?
cam kat oracle?
or just bole terus create table..
??
:-?
basically sume jenis database akn lalui step2 y sama..cuma nama function+connection string y akn berlainan..len suma sama..
ReplyDeletenext..kalu gune VS..no need to create user (utk beginner)..rite klik kat solution, add new item, add databse (either local or service databse)..mksure view skali databse explorer dlm VS and connection string..then bleh trus create table, connect and soon..
jgn install SQL Server Express ok..coz ia akn memeningkn anda..percaya lah..bile dh pndai utilize connection string bru gne SQL Server Express..
p/s : ni mesti xhabis usya VS lagik ni
kalu da open mesti close ka??
ReplyDeletekalu tak close??
statement tuk create/establish connection cam tak sama ngn oracle..
y part username,password bla4 tuh..