.net 串連ORACLE 資料庫的例子

來源:互聯網
上載者:User

利用 System.Data.OracleClient.Dll 的組件進行串連:

首先配置WebConfig 檔案;

<connectionStrings>
     <add name="oracleconn" connectionString="Data Source="";User ID=ryq;Password=123456" providerName="System.Data.OracleClient"/>
  </connectionStrings>

 

然後添加System.Data.OracleClient.Dll  的引用才可以在頁面中寫代碼using System.Data.OracleClient;

 

System.Data.OracleClient.Dll  常用的類:

System.Data.OracleClient.OracleConnection;

System.Data.OracleClient.OracleConnectionStringBuilder;

System.Data.OracleClient.OracleCommand;

System.Data.OracleClient.OracleCommandBuilder;

System.Data.OracleClient.OracleDataAdapter;

System.Data.OracleClient.OracleDataReader;

System.Data.OracleClient.OracleTransaction;

System.Data.OracleClient.OraclePerameter;

System.Data.OracleClient.OracleString;

System.Data.OracleClient.OracleType;

 

//前台顯示

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Oracle串連資料測試</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        顯示資料測試:<asp:Button ID="Btn_Search" runat="server" OnClick="Btn_Search_Click" Text="查詢"
            Width="64px" /><br />
        <br />
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="184px"
            Width="872px">
            <Columns>
                <asp:BoundField DataField="emporium_id" HeaderText="客戶編碼" SortExpression="客戶編碼" />
                <asp:BoundField DataField="item_no" HeaderText="項次" SortExpression="項次" />
                <asp:BoundField DataField="recipients" HeaderText="聯絡地址" SortExpression="聯絡地址" />
                <asp:BoundField DataField="delive_address" HeaderText="寄送地址" SortExpression="寄送地址" />
            </Columns>
        </asp:GridView>
   
    </div>
        <asp:Label ID="Label1" runat="server" Height="24px" Text="Label" Width="688px"></asp:Label>
    </form>
</body>
</html>

 

//後台代碼實現:

 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OracleClient; //要引用的類庫

public partial class _Default : System.Web.UI.Page
{
    public System.Data.OracleClient.OracleConnection objConn;  //聲明一個OracleConnection對象
    public System.Data.OracleClient.OracleCommand objCmd;  //聲明一個OracleCommand對象
    public System.Data.OracleClient.OracleDataAdapter objAdp; //資料庫適配器

    public System.Data.DataSet dset; //DataSet 用於資料填充
       //頁面載入
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "";
        string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["oracleconn"].ConnectionString;

        objConn = new OracleConnection(strconn);
    }
    //查詢按鈕設計
    protected void Btn_Search_Click(object sender, EventArgs e)
    {
        try
        {
            string StrSql = "select * from pub_address";
            objConn.Open();
            objCmd = new OracleCommand(StrSql, objConn);
            objAdp = new OracleDataAdapter(objCmd);
            dset = new DataSet();
            objAdp.Fill(dset);
            objConn.Close();
            GridView1.DataSource = dset;
            GridView1.DataBind();
        }
        catch(Exception ex)
        {
            Label1.Text = ex.ToString();
            return;
         }
    }
}

 

//以上簡單實現從Oracle 中查詢資料顯示GridView 中, 當然重要的是資料庫如何串連的部分。

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.