Asp.Net连接Oracle数据库的例子
2024-07-10 13:05:15
供稿:网友
asp.net连接oracle数据库的例子
刚接触asp.net,发现在 .net framework 1.1版中,直接就有system.data.oracleclient类库,
用于访问oracle数据库,不过我这里好像还是1.0 版本的,不支持该对象。
这里是一个通过oledb访问数据库的例子,与在asp中的差不多,能够访问大多数的数据库,比如access之类的。
<%@ page language="vb" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.oledb" %>
<script runat="server">
sub page_load(sender as object, e as eventargs)
dim connectionstring as string = "provider=oraoledb.oracle;user id=tmpuser;password=rt45ps1w;data source=sun450;"
dim commandtext as string = "select * from zscruser.sms_tk"
dim myconnection as new oledbconnection(connectionstring)
dim mycommand as new oledbcommand(commandtext, myconnection)
dim myreader as oledbdatareader
myconnection.open()
myreader=mycommand.executereader()
'way 1 use datagrid to show the return data
'直接绑定到datagrid控件
'datagrid1.datasource=myreader
'datagrid1.databind()
'或者像我们在asp中那样,通过一个循环输出数据,自己格式化外观
while myreader.read()
response.write(myreader("title") +"<br>")
'wend
end while
myreader.close()
myconnection.close()
end sub
</script>
<html>
<head>
</head>
<body >
<h2>simple data report</h2>
<hr size="1" />
<form runat="server">
<asp:datagrid id="datagrid1" runat="server" cellspacing="1" gridlines="none" cellpadding="3" backcolor="white" forecolor="black" enableviewstate="false">
<headerstyle font-bold="true" forecolor="white" backcolor="#4a3c8c"></headerstyle>
<itemstyle backcolor="#dedfde"></itemstyle>
</asp:datagrid>
</form>
</body>
</html>