我有一个从 Oracle DB 读取的 C# 应用程序,但在我的应用程序中没有数据。如果我直接从管理工具运行相同的 SELECT 语句,它会很好地返回数据。
static void Selectexample()
{
string connStr = @"Data Source=localhost;user id=system;password=123456";
DbProviderFactory dbFactory= DbProviderFactories.GetFactory("System.Data.OracleClient");
DbConnection con = dbFactory.CreateConnection();
con.ConnectionString = connStr;
string sql;
sql = "SELECT * FROM person";
DbCommand cmd = dbFactory.CreateCommand();
cmd.Connection = con;
cmd.CommandText = sql;
try
{
con.Open();
DbDataReader reader = cmd.ExecuteReader();
string headline = "";
for (int i = 0; i < reader.FieldCount; i++)
{
string s = reader.GetName(i);
headline += s.PadLeft(20) + " | ";
}
Console.WriteLine(headline);
while (reader.Read())
{
string line = "";
string s1 = (string)reader[0];
string s2 = (string)reader[1];
line += ("");
line += s1.PadLeft(20) + " | ";
line += s2.PadLeft(20) + " | ";
Console.WriteLine(line);
}
}
catch (DbException ex)
{
Console.WriteLine("fejl: " + ex.Message);
}
finally
{
con.Close();
}
Console.WriteLine();
Console.WriteLine("---");
}
Person 的表定义:
create table person
(
cpr char(10) primary key,
name varchar(25),
job varchar(25),
salary int,
zip char(4),
foreign key (zip) references zipcode(zip)
);
我在 x64 Windows 上运行带有 x86 Oracle 驱动程序的 .Net 3.0