asp.net连接access数据库
<%@ import namespace=”system.data” %>
<%@ import namespace=”system.data.oledb” %>
<script laguage=”vb” runat=”server”>
dim myconnection as oledbconnection
dim mycommand as oledbcommand
sub page_load(sender as object,e as eventargs)
1.连接数据库
dim dbname as string
dbname=server.mappath(“authors.mdb”)
myconnection = new oledbconnection( “provider=microsoft.jet.oledb.4.0;data source=”&dbname )
myconnection.open()
la1.text=”connection opened!”
2.添加记录
mycommand = new oledbcommand( “insert into authors(authors,country) values(simson,usa)”, myconnection )
mycommand.executenonquery()
la2.text=”new record inserted!”
3 更新数据(access)
mycommand = new oledbcommand( “update authors set authors=bennett where authors = simson”, myconnection )
mycommand.executenonquery()
la3.text=”record updated!”
4 删除数据(access)
mycommand = new oledbcommand( “delete from authors where authors = david”, myconnection )
mycommand.executenonquery()
la4.text=”record deleted!”
5 使用dategrid显示数据
mycommand = new oledbcommand( “select * from authors”, myconnection )
mydatagrid.datasource=mycommand.executereader()
mydatagrid.databind()
end sub
</script>
<html>
<body>
<asp:label id=”la1″ runat=”server” /><br>
<asp:label id=”la2″ runat=”server” /><br>
<asp:label id=”la3″ runat=”server” /><br>
<asp:label id=”la4″ runat=”server” /><br>
<asp:datagrid id=”mydatagrid” runat=”server”
bordercolor=”black”
borderwidth=”1″
gridlines=”both”
cellpadding=”3″
cellspacing=”0″
font-name=”verdana”
font-size=”10pt”
headerstyle-backcolor=”#aaaadd”
alternatingitemstyle-backcolor=”#eeeeee”
>
</asp:datagrid>
</body>
</html>