VB.Net学习笔记,使用ADO.Net对象访问数据库,将结果写入ListView
2024-07-10 13:03:13
供稿:网友
public class form1
inherits system.windows.forms.form
#region " windows 窗体设计器生成的代码 "
public sub new()
mybase.new()
'该调用是 windows 窗体设计器所必需的。
initializecomponent()
'在 initializecomponent() 调用之后添加任何初始化
end sub
'窗体重写处置以清理组件列表。
protected overloads overrides sub dispose(byval disposing as boolean)
if disposing then
if not (components is nothing) then
components.dispose()
end if
end if
mybase.dispose(disposing)
end sub
'windows 窗体设计器所必需的
private components as system.componentmodel.icontainer
'注意:以下过程是 windows 窗体设计器所必需的
'可以使用 windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
friend withevents button1 as system.windows.forms.button
friend withevents oledbconnection1 as system.data.oledb.oledbconnection
friend withevents oledbcommand1 as system.data.oledb.oledbcommand
friend withevents listview1 as system.windows.forms.listview
private sub initializecomponent()
me.button1 = new system.windows.forms.button()
me.oledbconnection1 = new system.data.oledb.oledbconnection()
me.oledbcommand1 = new system.data.oledb.oledbcommand()
me.listview1 = new system.windows.forms.listview()
me.suspendlayout()
'
'button1
'
me.button1.location = new system.drawing.point(296, 256)
me.button1.name = "button1"
me.button1.size = new system.drawing.size(80, 24)
me.button1.tabindex = 1
me.button1.text = "读取(&r)"
'
'listview1
'
me.listview1.location = new system.drawing.point(8, 8)
me.listview1.name = "listview1"
me.listview1.size = new system.drawing.size(384, 224)
me.listview1.tabindex = 2
'
'form1
'
me.autoscalebasesize = new system.drawing.size(6, 14)
me.clientsize = new system.drawing.size(408, 293)
me.controls.addrange(new system.windows.forms.control() {me.listview1, me.button1})
me.name = "form1"
me.text = "form1"
me.resumelayout(false)
end sub
#end region
'以上都不用看,是vs.net生成的,下面是我写的代码
private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click
dim datareader as system.data.oledb.oledbdatareader '定义datareader对象
dim i, j, fieldcount as integer
dim tempitem as listviewitem '定义临时listviewitem对象
oledbconnection1.connectionstring = "provider=microsoft.jet.oledb.4.0;data source=d:/inetpub/wwwroot/mydb.mdb" '设置连接字符串
oledbconnection1.open()
oledbcommand1.commandtext = "select * from mytable" '设置命令字符串
oledbcommand1.connection = oledbconnection1
datareader = oledbcommand1.executereader '执行sql语句,返回给datareader对象
fieldcount = datareader.fieldcount
listview1.fullrowselect = true
listview1.checkboxes = true
for i = 0 to fieldcount - 1 '枚举datareader中字段名,添加columns
listview1.view = view.details
listview1.columns.add(datareader.getname(i), 80, horizontalalignment.left)
next
while datareader.read() '获取记录信息,写入listview
tempitem = listview1.items.add(datareader.getvalue(0))
for j = 1 to fieldcount - 1
tempitem.subitems.add(datareader.getvalue(j) & "")
next
end while
datareader.close()
oledbconnection1.close()
end sub
end class
注册会员,创建你的web开发资料库,