把数据库中的一列数据读入一个数组,程序如下:
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var list = new List<double>(); //实例化一个list,先把要读的数据存放在list中,然后再填入数组
string constr = "User Id=haitao;Password=haitao;Data Source=orcl";
OracleConnection con = new OracleConnection(constr);
con.Open();
string cmdQuery = "select TOTAL_PASSENGER from LINE_PASSENGER t "; //将字段 TOTAL_PASSENGER 的数据存入数组
OracleCommand cmd = new OracleCommand(cmdQuery);
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
list.Add(reader.GetDouble(0));
}
reader.Dispose();
cmd.Dispose();
con.Dispose();
double[] allRecords = list.ToArray(); //将list的元素复制到数组中去
foreach (var item in allRecords)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}