C# 从C OracleDB运行SELECT时没有数据
我有一个C应用程序,它从Oracle数据库读取数据,但在我的应用程序中不获取任何数据。如果我直接从管理工具运行相同的SELECT语句,它将返回数据C# 从C OracleDB运行SELECT时没有数据,c#,oracle,C#,Oracle,我有一个C应用程序,它从Oracle数据库读取数据,但在我的应用程序中不获取任何数据。如果我直接从管理工具运行相同的SELECT语句,它将返回数据 static void Selectexample() { string connStr = @"Data Source=localhost;user id=system;password=123456"; DbProviderFactory dbFactory= DbProviderFactories.Get
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("---");
}
我在x64 Windows上运行带有x86 Oracle驱动程序的.Net 3.0谢谢大家的建议。事实证明,ManagementStudio没有在插入后立即提交数据。您必须在提交数据之前关闭该工具。现在,我的数据显示在我的应用程序中。代码看起来很好。报纸的标题会被打印出来吗?您是否在WhilerReader.Read循环中设置了断点?如果是这样的话,断点会被命中吗?请逐步执行调试器。上述代码对控制台的输出是什么?我怀疑问题在于成功连接到数据库。连接字符串指定数据源=本地主机。是否在TNSnames中设置了名为localhost的数据库?在SqlServer Management Studio中,如果更新数据,则必须将光标移到另一行,或重新运行select查询,以插入/更新数据。在甲骨文中可能也是如此
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)
);