C#读取链接到Oracle DB的Excel数据

时间:2014-01-31 17:16:22

标签: c# excel oracle11g

如何从链接到Oracle的Excel.xslx中读取数据?我可以使用C#读取整个工作表并将其打印到控制台窗口,但是这些被拉动(并经常刷新)的数据不能作为命名范围或表读取。我已经测试了读取数据作为其他工作簿中的命名范围/表(不是从Oracle中提取)就好了。我不想阅读整张表。所以我的问题是如何使用C#在我的sql查询中将此链接数据称为命名范围或表,或者是否有其他方法只能从Excel中读取链接数据?代码发布在下面,然后是输出错误(没有构建警告/错误),'PersonTable'是我给从oracle11g中提取的数据的命名范围。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace ReadExcelNamedRage
{
    class Program
    {
        static void Main(string[] args)
        {
            String connectionString = "Provider=Microsoft.Ace.OleDb.12.0; data source=c:\\documents and settings\\jhamandi\\desktop\\hrOracleTest.xlsx;extended properties=Excel 8.0";
            //Select using a Named Range
            string selectString = "SELECT * FROM PersonTable";

            //Select using a Worksheet name
           // string selectString = "SELECT * FROM [Sheet1$]";

            OleDbConnection con = new OleDbConnection(connectionString);
            OleDbCommand cmd = new OleDbCommand(selectString, con);

            try
            {
                con.Open();
                Console.WriteLine("Connection opened");
                OleDbDataReader theData = cmd.ExecuteReader();
                while (theData.Read())
                {
                    Console.WriteLine("{0}: {1} ({2}) - {3} ({4})", theData.GetDouble(0),
                        theData.GetString(1), theData.GetString(2), theData.GetString(3), theData.GetString(4));
                   // Console.WriteLine("Writing");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                con.Dispose();
                Console.WriteLine("Connection closed");
            }
        }
    }
}

连接已打开 Microsoft Access数据库引擎找不到对象PersonTable。嘛 确保对象存在,并拼写其名称和路径名称 年。如果PersonTable不是本地对象,请检查您的网络连接或续 扮演服务器管理员。 连接已关闭 按任意键继续 。 。

0 个答案:

没有答案