Mar
25
学会了VB中ADO的基本使用方法
蛮简单的。微软的东西总是傻瓜化了。。
简单记录一下如何读取、写入、删除。
读取:
private sub read_all()
dim conn as connection
dim rs as recordset
dim connstr, sql, dbname as string
set conn = new connection
dbname = app.path & "/dbname.mdb"
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbname
conn.open connstr
if err then
msgbox "Error Connecting!"
exit sub
end if
set rs = new recordset
sql = "select top 3 * from table1"
rs.open sql, conn, 1, 1
if err then
msgbox "Error querying!"
exit sub
end if
if rs.eof and rs.bof then
msgbox "No record"
exit sub
end if
label1.caption = ""
while not rs.eof
label1.caption = label1.caption & rs("id") & ", " & rs("name") & ", " & ..............
rs.movenext
wend
rs.close
end sub
增加(不全写了):
sql = "insert into table1 (name, age, ...) values ('name',12,...)"
rs.open sql, conn, 1, 3
或者:
sql = "select top 1 * from table1"
rs.open sql, conn, 1, 3
rs.addnew
rs("name") = "name"
rs("age") = 12
..
rs.update
rs.close
删除:
sql = "delete from table1 where id=" & id_num
rs.open sql, conn, 1, 3
转载请注明出自 ,如是转载文则注明原出处,谢谢:)
RSS订阅地址: https://www.felix021.com/blog/feed.php 。
简单记录一下如何读取、写入、删除。
读取:
private sub read_all()
dim conn as connection
dim rs as recordset
dim connstr, sql, dbname as string
set conn = new connection
dbname = app.path & "/dbname.mdb"
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbname
conn.open connstr
if err then
msgbox "Error Connecting!"
exit sub
end if
set rs = new recordset
sql = "select top 3 * from table1"
rs.open sql, conn, 1, 1
if err then
msgbox "Error querying!"
exit sub
end if
if rs.eof and rs.bof then
msgbox "No record"
exit sub
end if
label1.caption = ""
while not rs.eof
label1.caption = label1.caption & rs("id") & ", " & rs("name") & ", " & ..............
rs.movenext
wend
rs.close
end sub
增加(不全写了):
sql = "insert into table1 (name, age, ...) values ('name',12,...)"
rs.open sql, conn, 1, 3
或者:
sql = "select top 1 * from table1"
rs.open sql, conn, 1, 3
rs.addnew
rs("name") = "name"
rs("age") = 12
..
rs.update
rs.close
删除:
sql = "delete from table1 where id=" & id_num
rs.open sql, conn, 1, 3
欢迎扫码关注:
转载请注明出自 ,如是转载文则注明原出处,谢谢:)
RSS订阅地址: https://www.felix021.com/blog/feed.php 。