首页 > 编程 > .NET > 正文

asp.net 连接数据库基础

2024-07-10 13:03:06
字体:
来源:转载
供稿:网友


收集最实用的网页特效代码!

'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>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表