Example of. net connection to Oracle Database

Source: Internet
Author: User
Tags oracleconnection connectionstrings

Connect using the system. Data. oracleclient. dll component:

First, configure the webconfig file;

<Connectionstrings>
<Add name = "oracleconn" connectionstring = "Data Source =" "; user id = ryq; Password = 123456" providername = "system. Data. oracleclient"/>
</Connectionstrings>

 

Then you can add a reference to system. Data. oracleclient. DLL to write it on the page.CodeUsing system. Data. oracleclient;

 

Common classes of 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;

 

// Front-end display

 

<% @ 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 connection data test </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Br/>
Show data test: <asp: button id = "btn_search" runat = "server" onclick = "btn_search_click" text = "query"
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 = "customer No." sortexpression = "customer No."/>
<Asp: boundfield datafield = "item_no" headertext = "item" sortexpression = "item"/>
<Asp: boundfield datafield = "recipients" headertext = "contact address" sortexpression = "contact address"/>
<Asp: boundfield datafield = "delive_address" headertext = "shipping address" sortexpression = "shipping address"/>
</Columns>
</ASP: gridview>

</Div>
<Asp: Label id = "label1" runat = "server" Height = "24px" text = "label" width = "688px"> </ASP: Label>
</Form>
</Body>
</Html>

 

// Background code implementation:

 

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; // class library to be referenced

Public partial class _ default: system. Web. UI. Page
{
Public System. Data. oracleclient. oracleconnection objconn; // declare an oracleconnection object
Public System. Data. oracleclient. oraclecommand objcmd; // declare an oraclecommand object
Public System. Data. oracleclient. oracledataadapter objadp; // database Adapter

Public System. Data. dataset dset; // dataset is used for data filling.
// Page loading
Protected void page_load (Object sender, eventargs E)
{
Label1.text = "";
String strconn = system. configuration. configurationmanager. connectionstrings ["oracleconn"]. connectionstring;

Objconn = new oracleconnection (strconn );
}
// Query button Design
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;
}
}
}

 

// The preceding simple implementation queries data from Oracle and displays the data in the gridview. Of course, the important part is how the database is connected.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.