C# 如何从视图中获取数据

C# 如何从视图中获取数据,c#,sql,oracle,view,C#,Sql,Oracle,View,我有一个名为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_DETAIL

我有一个名为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
        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();
                }
            }
        }
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
我只想从这个视图中获取一行,查询结果如下

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
        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();
                }
            }
        }
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
运行此查询的C代码如下所示

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
        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();
                }
            }
        }
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
发现如果您试图执行一条SQL语句,该语句引用的表或视图不存在、您无权访问或属于另一个架构,并且您没有按架构名称引用该表,则通常会导致此错误

我检查了所有这些情况,但仍然无法解决问题。 让我知道我哪里做错了

对我没有帮助,所以不要把它标记为复制品

更新:我尝试将视图查询与选择查询相结合,结果成功了! 查询看起来像

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
        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();
                }
            }
        }
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

当我创建一个视图,然后尝试从视图中检索数据时,请告诉我为什么它不起作用。

Tablename xxx?@jarlh Tablename与视图名称相同,即string Tablename=EMP\u DETAILS;我也会更新同样的问题。您不也必须指定模式ABC吗?还是默认设置?@jarlh尝试指定模式,但没有用。这是否回答了您的问题?Tablename xxx?@jarlh Tablename与视图名称相同,即字符串Tablename=EMP\u DETAILS;我也会更新同样的问题。您不也必须指定模式ABC吗?还是默认设置?@jarlh尝试指定模式,但没有用。这是否回答了您的问题?