用DataSet方法读取数据
OracleHelper.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Data;
6 using System.Data.OracleClient;
7
8 namespace TestOracleOperation
9 {
10 class OracleHelper
11 {
12 private OracleConnection connection = null;
13
14 private void OpenDB(string connStr)
15 {
16 connection = new OracleConnection(connStr);
17
18 if (connection.State == ConnectionState.Closed)
19 {
20 connection.Open();
21 }
22 }
23
24 private void CloseDB()
25 {
26 if (connection != null && connection.State == ConnectionState.Open)
27 {
28 connection.Close();
29 connection.Dispose();
30 }
31
32 }
33
34 /// <summary>
35 /// 查询数据库
36 /// </summary>
37 /// <param name="sqlStr">查询语句</param>
38 /// <param name="connStr">连接数据库</param>
39 /// <param name="tableName">存放DataTable对象的</param>
40 /// <returns></returns>
41 public DataSet Query(string sqlStr, string connStr, string tableName)
42 {
43 DataSet dataSet = new DataSet();
44
45 try
46 {
47 OpenDB(connStr);
48
49 OracleCommand command = new OracleCommand(sqlStr, connection);
50
51 OracleDataAdapter dataAdapter = new OracleDataAdapter(command);
52
53 dataAdapter.Fill(dataSet, tableName);
54 }
55 finally
56 {
57 CloseDB();
58 Console.WriteLine(connection.State);
59 }
60
61 return dataSet;
62 }
63 }
64 }
Program.cs
1 using System;
2 using System.Data;
3
4 namespace TestOracleOperation
5 {
6 class Program
7 {
8 static void Main(string[] args)
9 {
10 string conStr = "Data Source=testOracle;User Id=test;Password=test;";
11 string sqlComm = "SELECT * FROM users where rownum<10";
12
13 OracleHelper oracleHelper = new OracleHelper();
14
15 DataSet accountSet = oracleHelper.Query(sqlComm, conStr, "users ");
16
17 foreach (DataRow row in accountSet.Tables["users "].Rows)
18 {
19 Console.WriteLine(row["user_id"] + "\t" + row["user_passport"]);
20 }
21
22 Console.ReadKey();
23
24 }
25 }
26 }
参考:http://www.cnblogs.com/dongdonghuihui/archive/2009/08/13/1545446.html