日期:2011-11-16  浏览次数:20616 次



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 style="FONT-FAMILY: arial">

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