'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>
新聞熱點
疑難解答
圖片精選