C # Methods to connect Oracle databases

Source: Internet
Author: User
Tags count connect oracleconnection rollback tostring

1. Import Reference System.Data.OracleClient.dll file

2, in the head using System.Data.OracleClient;

To read data through a dataset:

To create a connection to a database

OracleConnection oracon=new oracleconnection ("User Id=112;data source=wmatech;password=112");

Create a new DataAdapter to populate the dataset

OracleDataAdapter oradap=new OracleDataAdapter ("select * from actor", Oracon);

Create a new dataset

DataSet ds=new DataSet ();

Fill DataSet

Oradap.fill (DS);

Create a new DataTable

DataTable _table=ds. Tables[0];

To view the number of columns of data in a table

int count=_table. Rows.Count;

datagrid1.datasource=_table;

Datagrid1.databind ();

To read data by DataReader:

To create a connection to a database

OracleConnection oracon=new oracleconnection ("User Id=112;data source=wmatech;password=112");

Create a new instance of the database operation

OracleCommand oracmd=new OracleCommand ("select * from actor", Oracon);

Open a database connection

Oracon.open ();

DataReader provides a way to read only incoming streams of rows from a database.

OracleDataReader orard= Oracmd.executereader ();

String Szhtml= "";

while (Orard.read ())

{

Szhtml+=orard[0]. ToString () + "" +orard[1]. ToString () + "" +orard[2]. ToString () + "

";

}

Orard.close ();

To close a database connection

Oracon.close ();

Response.Write (szhtml);

Manipulating the database by command

To create a connection to a database

OracleConnection oracon=new oracleconnection ("User Id=112;data source=wmatech;password=112");

Create a new instance of the database operation

OracleCommand oracmd=new OracleCommand ("update actor set name= ' 123453 ' where id= ' admin '", oracon);

Oracon.open ();

EXECUTENONQUERY executes Transact-SQL statements against the connection and returns the number of rows affected.

int Effnum=oracmd.executenonquery ();

Response.Write (Effnum. ToString ());

Oracon.close ();

About the use of transaction functions

To create a connection to a database

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/csharp/

OracleConnection oracon=new oracleconnection ("User Id=112;data source=wmdb;password=112");

OracleCommand oracmd=new OracleCommand ();

Open connection

Oracon.open ();

Create a new instance of a transaction object

OracleTransaction oratact=oracon.begintransaction ();

Oracmd.connection=oracon;

Bind transaction object to command

Oracmd.transaction=oratact;

Try

{

Inserts the specified column of a table's row that satisfies a condition into another table

oracmd.commandtext= INSERT INTO Yz_tranetp_probpow (Oid,parentid,shiptype,amount,ton,weight,custnum,cubmeter, Standbox,shipchara,busrange) Select Oid,parentid,shiptype,amount,ton,weight,custnum,cubmeter,standbox,shipchara, Busrange from Yz_tranetp_probpow_app where seqappid= ' "+appid+" ' and needupdated=1 ';

Oracmd.executenonquery ();

oracmd.commandtext= "Update Yz_tranetp_probpow_app set needupdated=0 where seqappid= '" +appid+ "";

Oracmd.executenonquery ();

No errors, execute Submit command

Oratact.commit ();

}

catch (Exception ex)

{

Error occurred, execute rollback command

Oratact.rollback ();

Pop-up window display error

Response.Write ("");

}

Finally

{

Close connection

Oracon.close ();

}

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.