4、 声明类级的变量。找到设计期产生的代码部分,把下面的语句加到变量声明后面。什么?不知道变量声明在哪儿?不会吧!
private fs as filestream
private ds as dataset
private conn as new sqlconnection("server=localhost;database=northwind;integrated security=true;")
private currentpos as integer = 9
5、 开始写代码了。首先是form_load
private sub form1_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
btnopen.enabled = true
btnsave.enabled = true
btnback.enabled = false
btnforward.enabled = false
end sub
6、 open按钮的点击事件代码:
private sub openbtn_click(byval sender as system.object, byval e as system.eventargs) handles btnopen.click
dim opendlg as new openfiledialog
opendlg.title = "select a picture file"
opendlg.filter = "(*.bmp)|*bmp|(*.jpg)|*.jpg"
opendlg.showdialog()
lblpath.text = opendlg.filename.tostring()
fs = new filestream(opendlg.filename.tostring(), filemode.open, fileaccess.read)
picturebox1.image = image.fromfile(opendlg.filename.tostring())
end sub
7、 save按钮的点击事件代码
private sub savebtn_click(byval sender as system.object, byval e as system.eventargs) handles btnsave.click
dim sqlcomm as new sqlcommand
sqlcomm.commandtext = "insert into employees (lastname,firstname,photo) values (@lastname,@firstname,@photo)"
sqlcomm.connection = conn
dim data(fs.length) as byte
fs.read(data, 0, int(fs.length))
dim prm1 as new sqlparameter("@lastname", txtln.text)
dim prm2 as new sqlparameter("@firstname", txtfn.text)
dim prm3 as new sqlparameter("@photo", sqldbtype.varbinary, int(fs.length), parameterdirection.input, false, 0, 0, "", datarowversion.current, data)
sqlcomm.parameters.add(prm1)
sqlcomm.parameters.add(prm2)
sqlcomm.parameters.add(prm3)
try
conn.open()
sqlcomm.executenonquery() 执行插入语句
conn.close()
fs.close()
catch ex as exception
msgbox(ex.message)
end try
end sub
8、 选择view状态的事件代码
private sub checkbox1_checkedchanged(byval sender as system.object, byval e as system.eventargs) handles checkbox1.checkedchanged
if checkbox1.checked = true then
btnopen.enabled = false
btnsave.enabled = false
btnback.enabled = true
btnforward.enabled = true
currentpos = 9
dim sqlcomm as new sqlcommand
sqlcomm.commandtext = "select employeeid, photo from employees order by employeeid"
sqlcomm.connection = conn
dim da as new sqldataadapter(sqlcomm)
try
conn.open()
ds = new dataset
da.fill(ds, "employees")
conn.close()
catch sqlex as sqlexception
msgbox(sqlex.message)
end try
dim data() as byte = ds.tables("employees").rows(9)("photo")
dim stmphoto as new memorystream(data)
picturebox1.image = image.fromstream(stmphoto)
else
btnopen.enabled = true
btnsave.enabled = true
btnback.enabled = false
btnforward.enabled = false
end if
end sub
9、 “>>”按钮点击事件代码
private sub forward_click(byval sender as system.object, byval e as system.eventargs) handles btnforward.click
if currentpos = ds.tables("employees").rows.count – 1 then
return
else
currentpos += 1
dim data() as byte
data = ds.tables("employees").rows(currentpos)("photo")
dim stmphoto as new memorystream(data)
picturebox1.image = image.fromstream(stmphoto)
end if
end sub
10、 “<<”按钮点击事件代码
private sub back_click(byval sender as system.object, byval e as system.eventargs) handles btnback.click
if currentpos = 9 then
return
else
currentpos -= 1
end if
dim data() as byte
data = ds.tables("employees").rows(currentpos)("photo")
dim stmphoto as new memorystream(data)
picturebox1.image = image.fromstream(stmphoto)
end sub
11、 好了,可以运行看看了。