using System;
using System.Data.OleDb;
using System.Diagnostics;
namespace PullData
{
class Program
{
static void Main(string[] args)
{
OleDbConnection connLocal = new OleDbConnection("Provider=SAOLEDB.10;Data Source=dental;Persist Security Info=True;User ID=dba;PWD=sql;Location=1.2.3.4");
OleDbCommand cmdLocal = new OleDbCommand("SELECT tran_num, amount, tran_date FROM transactions", connLocal);
try
{
connLocal.Open();
}catch (Exception connerr) { Debug.WriteLine(connerr.Message);}
OleDbDataReader readLocal = cmdLocal.ExecuteReader();
while (readLocal.Read())
{
Console.WriteLine(readLocal.GetString(0), readLocal.GetString(1), readLocal.GetString(2));
}
readLocal.Close();
connLocal.Close();
if (Debugger.IsAttached)
{
Console.ReadLine();
}
}
上面是我试图在Visual Studio 2015中运行的代码,我尝试连接到旧的SQL Anywhere 10 DB,连接字符串直接来自VS2015的数据库资源管理器,它连接到查看表(我添加PWD
除外)。这只是查看表格内容的垫脚石,因此我可以稍后将查询写入CSV或文本。
我在尝试调试/运行时收到的错误是:
An unhandled exception of type 'System.InvalidCastException' occurred in System.Data.dll
Additional information: Specified cast is not valid.
指向:
Console.WriteLine(readLocal.GetString(0), readLocal.GetString(1), readLocal.GetString(2));
似乎没有认识到读者。有人有什么建议吗?谢谢!