Is this possible to execute a View from C# code. If it is then I would like to know if parametrized views exists and how should I use them. Parametrized means same parameters we use in Stored Procedures for stating where conditions.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
One does not execute views. One executes a query. If the query selects rows from a view or from a table, ADO.NET doesn't know or care.
回答2:
You treat a view the same way you would treat a table (for selecting, that is).
A parameterized query that would use a table in the FROM
clause would work just as well with a view.
Don't confuse views with stored procedures - stored procedures are executed, views simply are. You read data from a view through a query, using a SELECT
statement.
回答3:
Think of a veiw as straight SQL where the View name replaces a table name: i.e. select * from v_employee_department...vs...select * from employee where v_employee_edpartment is a view that joins the employee table and department table
// Declare connection string.
string connStr = Properties.Settings.Default.ConnectionString;
OracleConnection cn = new OracleConnection(connStr);
// STEP 1: Execute command
string selectCommandTotal = "SELECT ID FROM <SOME_VIEW> WHERE <SOME_FIELD> = <SOME_VALUE> ";
OracleCommand cmdGetTotals = new OracleCommand(selectCommandTotal, cn);
cmdGetTotals.Connection.Open();
OracleDataReader rdrGetTotals = cmdGetTotals.ExecuteReader();