ADO. NET and Oracle (1): Get multi-row record set, ado. netoracle
I recently came into contact with ADO. NET and Oracle to share some of my common problems with you.
I think the most basic requirement for getting started with ADO. NET to operate Oracle is to update data in Oracle through ADO. NET.
This document describes how to update a database using Command objects.
All content is based on VS2010 and Oracle11g (32bit)
1. Connect to the database
Database connection depends on OracleConnection object
OracleConnection con = new OracleConnection("data source=orcl;uid=system;pwd=********");
Orcl is the name of the database, which is the default value when oracle is installed.
After the Database is installed, you can also use the Database Configuration Assistant tool to delete or create a Database.
Uid is the login name, pwd is the Login Password
To connect to the database, run the con. Open () method.
To disconnect a connection, you only need to execute the con. Close () or con. Dispose () method.
2. operate databases
After connecting to the database, you can operate the database.
Start with the simplest operation, that is, return all records from the database.
Create a product table in the database
As follows:
ID |
Name |
Price |
Count |
001 |
Apple |
6 |
3 |
002 |
Orange |
5 |
4 |
003 |
Pear |
4 |
2 |
The table above records the IDs, unit prices, and quantity of apples and oranges.
Both ID and Name are varchar data, while Price and Count are Number data.
Now try to read all the data in the table and then display the data on the screen completely.
The Code is as follows:
Using System; using System. collections. generic; using System. linq; using System. text; using System. data; using System. data. oracleClient; namespace DataReader {class Program {static void Main (string [] args) {try {using (OracleConnection con = new OracleConnection ("data source = orcl; uid = system; pwd = wu92890910xu ") {using (OracleCommand cmd = new OracleCommand (" select * from product ", con) {con. open (); OracleDataReader dataReader = cmd. executeReader (); if (dataReader. hasRows) Console. writeLine ("read data... "); else Console. writeLine ("data not read... "); while (dataReader. read () {Console. writeLine (dataReader. getString (1) + ":" + dataReader. getDouble (2) * dataReader. getDouble (3) ;}}} catch (Exception ex) {Console. writeLine (ex. message);} Console. readKey ();}}}
You can try to change select * from product to a statement such as select id and name from product.
Appendix: to operate Oracle using ADO. NET, the namespace must be included.
Using System. Data. OracleClient;
The namespace is provided by Oracle and is not provided by NET itself.
If you need that. dll file, you can e-mail: cf520wuxu@outlook.com