Saturday, November 3, 2012

contoh program visual basic

import system.data.sqlclient
module module1
sub main()
dim constring,db,user,pwd as string
pwd

'set up query
dim sql as string="select*from tbbarang"

'set up dml
dim ins as string
sqlins="insert into tbbarang(kodebarang,namabarang,satuan,hargabeli,hargajual)"
sqlins=sqlins&"values(@vkode,@vnama,@vsatuan,@vbeli,@vjual)"

'isi variabel sebagai data mentah
dim vkode as string="BR002"
dim vnama as string="sepeda"
dim vsatuan as string="pcs"
dim vbeli as single=100000
dim vjaul as single=150000

'menciptakan string query
dim sqlqry as string="select count(*)from tbbarang

'menciptakan pernyataan insert
dim sqlins as string
sqlins="insert into tbbarang(kodebarang,namabarang,satuan,hargabeli,hargajual)"
sqlins=sqlins&"values(@vkode,@vnama,@vsatuan,@vbeli,@vjual)"

'menciptakan pernyataan delete
dim sqldel as string
sqldel="delete from tbbarang where"
sqldel=sqldel&"kodebarang=@vkode"
sqldel=sqldel&"and satuan=@satuan"

'menciptakan connection
dim conn as sqlconnection=new sqlconnection(connstring)

try
'membuka connection
conn.open()

'menciptakan data adapter
dim da as sqladapter=new sqladapter(sql,conn)

'menciptakan dan mengisi dataset
dim ds as dataset=new dataset()
da.fill(ds,"tbbarang")

'mendapatkan referensi table
dim dt as datatable=ds.tables("tbbarang")

'menampilkan baris dari data table sebelum ditambah
console.writeline("sebelum ditambah")

for each row as datarow in dt.row
    console.wrilteline("{0}{1}{2}{3}",_
    row("kodebarang").tostring().padright(6),_
    row("namabarang").tostring().padright(51),_
    row("satuan"),row("hargabeli))
next

'menambah baris
dim newrow as datarow=dt.newrow()
newrow("kodebarang")="cpu01"
newrow("namabarang")="cpubaru"
newrow("satuan")="unit"
newrow("hargabeli")=200000
newrow("hargajual")=250000
dt.row.add(newrow)

'menampilkan baris dari data table setelah ditambah
console.writeline("setelah ditambah")
for each row as datarow in dt.rows
    console.wrilteline("{0}{1}{2}{3}",_
    row("kodebarang").tostring().padright(6),_
    row("namabarang").tostring().padright(51),_
    row("satuan"),row("hargabeli))
next

'menambah barang
'menciptakan command
dim cmd as sqlcommand=new sqlcommand(ins,con)

'memetakan parameter
cmd.parameter.add("@vkode",sqldbtype.char,5,"kodebarang")
cmd.parameter.add("@vnama",sqldbtype.char,50,"namabarang")
cmd.parameter.add("@vsatuan",sqldbtype.char,5,"satuan")
cmd.parameter.add("@vbeli",sqldbtype.decimal,18,"hargabeli")
cmd.parameter.add("@vjual",sqldbtype.decimal,18,"hargajual")
'update database
    da.insertcommand=cmd
    da.update(ds,"tbbarang")
catch ex as exception

'menampilkan pesan kesalahan
console.writeline("error":&ex.tostring)
finally

'menutup connection
console.write("tekan enter untuk melanjutkan")
console.readline()
conn.close()
end try

end sub

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
Twitter Delicious Facebook Digg Stumbleupon Favorites More