c# - 如何从视图中获取数据
问题描述
我有一个名为的视图EMP_DETAILS
,看起来像
CREATE OR REPLACE FORCE VIEW "ABC"."EMP_DETAILS" ("NAME", "COUNTRY") AS
SELECT
b.NAME,c.COUNTRY
FROM
ABC.Emp a ,ABC.Emp_Bom b ,ABC.Emp_info c
where a.E_ID=b.ID and a.R_ID=c.NR order by 1 asc;
我只是希望从此视图中获取一行,并且相同的查询看起来像
Select * from EMP_DETAILS where NAME = 'xxx' and COUNTRY = 'xxx'; //Query works fine in Oracle SQL Developer
运行此查询的 C# 代码如下所示
DataSet ds = null;
OracleDataAdapter adapter = null;
StringBuilder builder = new StringBuilder();
builder.Append("SELECT * from EMP_DETAILS where NAME = :xx and COUNTRY = :xxx");
command = new OracleCommand(builder.ToString());
command.Parameters.Add("xx", name);
command.Parameters.Add("xxx", country);
string tablename = "EMP_DETAILS";
using (OracleConnection oc = new OracleConnection(CONNECTIONSTRING))
{
try
{
adapter = new OracleDataAdapter();
command.CommandType = CommandType.Text;
command.Connection = oc;
adapter.SelectCommand = command;
ds = new DataSet();
adapter.Fill(ds, tablename); //Code breaks here msg says **No table or view found**
}
catch(Exception ex)
{
if (ds != null)
{
ds.Dispose();
ds = null;
}
}
finally
{
if (oc != null)
{
oc.Close();
oc.Dispose();
}
}
}
发现此错误通常是由于 您尝试执行引用不存在、您无权访问或属于另一个模式的表或视图的 SQL 语句,并且您没有通过以下方式引用该表架构名称。
我检查了所有这些情况,但仍然无法解决问题。让我知道我哪里出错了。
这个问题对我没有帮助,所以不要将其标记为重复。
更新:我尝试将view query
与我的结合起来Select query
,它可以工作!查询看起来像
select* from
(SELECT b.NAME, c.COUNTRY FROM
ABC.Emp a ,ABC.Emp_Bom b ,ABC.Emp_info c
where a.E_ID=b.ID and a.R_ID=c.NR order by 1 asc)
where NAME LIKE :xx and AREA LIKE :xxx
让我知道为什么当我创建视图然后尝试从视图中检索数据时它不起作用。