Sei sulla pagina 1di 7

Univerzitet u Sarajevu

Elektrotehniki fakultet Sarajevo


Odsjek za raunarstvo i informatiku

DAO - DAL

1.SkiniteWAMP(apachemysqlphp)sa(imainstaliranuselabu):
http://www.wampserver.com/en/#downloadwrapper

onevaminstaliratimysqlbazupodataka.

Otvoritemysqlkonzolu:

iumjestopasswordasamopritisniteEnterdugme.

dakreiratebazuukucajte:

zatimukucajte
usedao
daizaberetebazu
te
createtablestudent(idintauto_incrementprimarykey,imevarchar(50),prezime
varchar(50))
dakreiratetabelustudent.
daprovjeritepodatkeutabeliukucajte
select*fromstudent

dadodate:
insertintostudentvalues(hapo,hapic)

tj.

KREIRANJEDAO/DAL
Nakonkreiranjabazeitabelepotrebnojenapravitiaplikacijukojaepristupitiovojbaziiunositi
noveslogove.

DabistesekonektovalinaMySqlbazupodatakaizC#potrebanvamjekonektor(.DLL)za
uspostavljanjekonekcijekojegmoratereferenciratiuvaemprojektutegamoetepreuzetisa:
http://dev.mysql.com/get/Downloads/ConnectorNet/mysqlconnectornet6.8.3.msi

(nijesepotrebnoregistrovatizadownload)

Nakoninstalacije

KreirajtenoviConsoleApplicationireferencirajteMysql.Data.dll

KonekcijaizC#anaMySqlbazupodataka

usingMySql.Data.MySqlClient

stringuser=root
stringpass=
stringdb=dao
stringconnectionString="server=localhostuser="+user+"pwd="+pass+"database="+db
MySqlConnectioncon=newMySqlConnection(connectionString)
//Unosnovogstudenata
con.Open()
stringime=hamo1,prezime=hapic1
MySqlCommandc=newMySqlCommand("insertintostudentvalues('"+ime+"','"+prezime+"')",con)
c.ExecuteNonQuery()

//Ucitavanjestudenataulistu
List<Student>studenti=newList<Student>()
c=newMySqlCommand("select*fromstudent",con)
MySqlDataReaderr=c.ExecuteReader()
while(r.Read())
studenti.Add(newStudent(r.GetString("ime"),r.GetString("prezime")))

Naravnoovosveideutrycatch:)

KreirajteentitetStudent.cs

KlasastudentPrimjer:

usingSystem
usingSystem.Collections.Generic

namespaceDAL.Entiteti
{
publicclassStudent
{
longid
stringime,prezime
List<Single>ocjene=newList<Single>()

publicStudent(stringi,stringp){
ime=i
prezime=p
}

publicstringIme
{
get
set
}
publicstringPrezime
{
get
set
}
publiclongId
{
get
set
}
}
}

KreirajteDAO(dataaccessobject)zastudentaStudentDAOkojiimplementiraCRUD(create
readupdatedelete)interfejs:

IDaoCrud.cs
usingSystem
publicinterfaceIDaoCrud<T>
{
longcreate(Tentity)//INSERTINTO
Tread(Tentity)//SELECTFROMWHERE..
Tupdate(Tentity)//UPDATE
voiddelete(Tentity)//DELETE

TgetById(intid)//SELECT..WHREID=
List<T>getAll()//SELECT*
List<T>getByExample(stringname,stringvalue)//SELECT..WHEREname=value
}

StudentDAO.cs
publicclassStudentDAO:IDaoCrud<Student>{
privateMySqlConnectioncon

publicStudentDAO(stringhost,stringdb,stringuser,stringpass){
stringconnectionString=
"server=localhostuser="+user+"pwd="+pass+"database="+db
con=newMySqlConnection(connectionString)
try{
con.Open()
}catch(Exceptione){throwe}
}
longcreate(Studententity){

//insertinto...
}
//CRUDmetodeimplementirane
}

iimplementirajtemapiranjesabazom

Dabinaprojekatbioelegantniji,preglednijiidamoemolaganododavatinoveentitetetabele
implementiratiemoDAL/DAO(DataAccessLayer/DataAccessObject)patternkaoi
CRUD(Create/Read/Update/Delete)prikazannadijagramuklasa:

ZADATAK
Skinuti:
http://dev.mysql.com/get/Downloads/MySQLGUITools/mysqlworkbenchcommunity6.1.4win32
.msi

generisatibazuizERDa