Executing procedure from Visual Studio
up vote
-1
down vote
favorite
I'm trying to execute plsql procedure from Visual Studio. Basically what I'm trying to do is pass some parameters and using them I'm trying to insert values into a different table. The procedure uses a cursor and inserts values into a table and after that I'm deleting that table to make use of it for the next time. The table obtained using cursor needs to be finally displayed on gridbox. But the below program isn't displaying anything. Can someone help me with this?
DB_connect();
String x3 = "google";
String x1;
String x2;
String s1 = "delete from temp";
OracleCommand comm = new OracleCommand(s1, conn);
comm.ExecuteNonQuery();
System.Data.OracleClient.OracleCommand comm2 = new System.Data.OracleClient.OracleCommand();
String s2 = "exec cv";
comm2.CommandText = s2;
comm2.CommandType = CommandType.StoredProcedure;
comm2.Parameters.Add("x", System.Data.OracleClient.OracleType.Number).Value = comboBox1.Text;
comm2.Parameters.Add("y", System.Data.OracleClient.OracleType.Number).Value = comboBox2.Text;
comm2.ExecuteNonQuery();
String s3 = "select * from temp";
OracleCommand comm3 = new OracleCommand(s3, conn);
OracleDataAdapter MyAdapter3 = new OracleDataAdapter();//adapter acts as interface btw database and dataset(which is collectio of tables)
MyAdapter3.SelectCommand = comm;
DataTable dTable3 = new DataTable();//datatable represents a single table in database
MyAdapter3.Fill(dTable3);
dataGridView1.DataSource = dTable3;
conn.Close();
This is my stored procedure:
create or replace procedure cv(x in int, y in int, z in varchar)
as
cursor c
is
select email, collegename, cgpa, compname
from student_cv
where (cgpa >= x and yearsofexp >= y) and compname = z;
tem c%rowtype;
begin
open c;
loop
fetch c into tem;
exit when c%notfound;
insert into temp
values(tem.email, tem.collegename, tem.cgpa, tem.compname);
end loop;
end;
/
Edit:

c# sql .net oracle visual-studio
add a comment |
up vote
-1
down vote
favorite
I'm trying to execute plsql procedure from Visual Studio. Basically what I'm trying to do is pass some parameters and using them I'm trying to insert values into a different table. The procedure uses a cursor and inserts values into a table and after that I'm deleting that table to make use of it for the next time. The table obtained using cursor needs to be finally displayed on gridbox. But the below program isn't displaying anything. Can someone help me with this?
DB_connect();
String x3 = "google";
String x1;
String x2;
String s1 = "delete from temp";
OracleCommand comm = new OracleCommand(s1, conn);
comm.ExecuteNonQuery();
System.Data.OracleClient.OracleCommand comm2 = new System.Data.OracleClient.OracleCommand();
String s2 = "exec cv";
comm2.CommandText = s2;
comm2.CommandType = CommandType.StoredProcedure;
comm2.Parameters.Add("x", System.Data.OracleClient.OracleType.Number).Value = comboBox1.Text;
comm2.Parameters.Add("y", System.Data.OracleClient.OracleType.Number).Value = comboBox2.Text;
comm2.ExecuteNonQuery();
String s3 = "select * from temp";
OracleCommand comm3 = new OracleCommand(s3, conn);
OracleDataAdapter MyAdapter3 = new OracleDataAdapter();//adapter acts as interface btw database and dataset(which is collectio of tables)
MyAdapter3.SelectCommand = comm;
DataTable dTable3 = new DataTable();//datatable represents a single table in database
MyAdapter3.Fill(dTable3);
dataGridView1.DataSource = dTable3;
conn.Close();
This is my stored procedure:
create or replace procedure cv(x in int, y in int, z in varchar)
as
cursor c
is
select email, collegename, cgpa, compname
from student_cv
where (cgpa >= x and yearsofexp >= y) and compname = z;
tem c%rowtype;
begin
open c;
loop
fetch c into tem;
exit when c%notfound;
insert into temp
values(tem.email, tem.collegename, tem.cgpa, tem.compname);
end loop;
end;
/
Edit:

c# sql .net oracle visual-studio
Have you tested that the procedure actually returns any values?
– stuartd
2 days ago
my procedure is working in sql plus but I'm getting this error while executing this from visual studio
– portgas. D. Ace
2 days ago
2
On a general note, it's better to just run a query, or a view, or even to make a function that returns an object/rowtype, or a table of objects, or even a refcursor which you can open and read. Writing temporary data just for the sake of reading it on the other side should be the last fallback.
– GolezTrol
2 days ago
Open your connection:conn.Open();before you execute any operations against the data store. Also it is not clear whereconnresides. Is this a global (static) field somewhere? If so maybe there is a race condition where multiple threads are trying to open/close this connection at the same time (like a web application with multiple requests).
– Igor
2 days ago
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I'm trying to execute plsql procedure from Visual Studio. Basically what I'm trying to do is pass some parameters and using them I'm trying to insert values into a different table. The procedure uses a cursor and inserts values into a table and after that I'm deleting that table to make use of it for the next time. The table obtained using cursor needs to be finally displayed on gridbox. But the below program isn't displaying anything. Can someone help me with this?
DB_connect();
String x3 = "google";
String x1;
String x2;
String s1 = "delete from temp";
OracleCommand comm = new OracleCommand(s1, conn);
comm.ExecuteNonQuery();
System.Data.OracleClient.OracleCommand comm2 = new System.Data.OracleClient.OracleCommand();
String s2 = "exec cv";
comm2.CommandText = s2;
comm2.CommandType = CommandType.StoredProcedure;
comm2.Parameters.Add("x", System.Data.OracleClient.OracleType.Number).Value = comboBox1.Text;
comm2.Parameters.Add("y", System.Data.OracleClient.OracleType.Number).Value = comboBox2.Text;
comm2.ExecuteNonQuery();
String s3 = "select * from temp";
OracleCommand comm3 = new OracleCommand(s3, conn);
OracleDataAdapter MyAdapter3 = new OracleDataAdapter();//adapter acts as interface btw database and dataset(which is collectio of tables)
MyAdapter3.SelectCommand = comm;
DataTable dTable3 = new DataTable();//datatable represents a single table in database
MyAdapter3.Fill(dTable3);
dataGridView1.DataSource = dTable3;
conn.Close();
This is my stored procedure:
create or replace procedure cv(x in int, y in int, z in varchar)
as
cursor c
is
select email, collegename, cgpa, compname
from student_cv
where (cgpa >= x and yearsofexp >= y) and compname = z;
tem c%rowtype;
begin
open c;
loop
fetch c into tem;
exit when c%notfound;
insert into temp
values(tem.email, tem.collegename, tem.cgpa, tem.compname);
end loop;
end;
/
Edit:

c# sql .net oracle visual-studio
I'm trying to execute plsql procedure from Visual Studio. Basically what I'm trying to do is pass some parameters and using them I'm trying to insert values into a different table. The procedure uses a cursor and inserts values into a table and after that I'm deleting that table to make use of it for the next time. The table obtained using cursor needs to be finally displayed on gridbox. But the below program isn't displaying anything. Can someone help me with this?
DB_connect();
String x3 = "google";
String x1;
String x2;
String s1 = "delete from temp";
OracleCommand comm = new OracleCommand(s1, conn);
comm.ExecuteNonQuery();
System.Data.OracleClient.OracleCommand comm2 = new System.Data.OracleClient.OracleCommand();
String s2 = "exec cv";
comm2.CommandText = s2;
comm2.CommandType = CommandType.StoredProcedure;
comm2.Parameters.Add("x", System.Data.OracleClient.OracleType.Number).Value = comboBox1.Text;
comm2.Parameters.Add("y", System.Data.OracleClient.OracleType.Number).Value = comboBox2.Text;
comm2.ExecuteNonQuery();
String s3 = "select * from temp";
OracleCommand comm3 = new OracleCommand(s3, conn);
OracleDataAdapter MyAdapter3 = new OracleDataAdapter();//adapter acts as interface btw database and dataset(which is collectio of tables)
MyAdapter3.SelectCommand = comm;
DataTable dTable3 = new DataTable();//datatable represents a single table in database
MyAdapter3.Fill(dTable3);
dataGridView1.DataSource = dTable3;
conn.Close();
This is my stored procedure:
create or replace procedure cv(x in int, y in int, z in varchar)
as
cursor c
is
select email, collegename, cgpa, compname
from student_cv
where (cgpa >= x and yearsofexp >= y) and compname = z;
tem c%rowtype;
begin
open c;
loop
fetch c into tem;
exit when c%notfound;
insert into temp
values(tem.email, tem.collegename, tem.cgpa, tem.compname);
end loop;
end;
/
Edit:

c# sql .net oracle visual-studio
c# sql .net oracle visual-studio
edited 2 days ago
marc_s
564k12510881240
564k12510881240
asked 2 days ago
portgas. D. Ace
123
123
Have you tested that the procedure actually returns any values?
– stuartd
2 days ago
my procedure is working in sql plus but I'm getting this error while executing this from visual studio
– portgas. D. Ace
2 days ago
2
On a general note, it's better to just run a query, or a view, or even to make a function that returns an object/rowtype, or a table of objects, or even a refcursor which you can open and read. Writing temporary data just for the sake of reading it on the other side should be the last fallback.
– GolezTrol
2 days ago
Open your connection:conn.Open();before you execute any operations against the data store. Also it is not clear whereconnresides. Is this a global (static) field somewhere? If so maybe there is a race condition where multiple threads are trying to open/close this connection at the same time (like a web application with multiple requests).
– Igor
2 days ago
add a comment |
Have you tested that the procedure actually returns any values?
– stuartd
2 days ago
my procedure is working in sql plus but I'm getting this error while executing this from visual studio
– portgas. D. Ace
2 days ago
2
On a general note, it's better to just run a query, or a view, or even to make a function that returns an object/rowtype, or a table of objects, or even a refcursor which you can open and read. Writing temporary data just for the sake of reading it on the other side should be the last fallback.
– GolezTrol
2 days ago
Open your connection:conn.Open();before you execute any operations against the data store. Also it is not clear whereconnresides. Is this a global (static) field somewhere? If so maybe there is a race condition where multiple threads are trying to open/close this connection at the same time (like a web application with multiple requests).
– Igor
2 days ago
Have you tested that the procedure actually returns any values?
– stuartd
2 days ago
Have you tested that the procedure actually returns any values?
– stuartd
2 days ago
my procedure is working in sql plus but I'm getting this error while executing this from visual studio
– portgas. D. Ace
2 days ago
my procedure is working in sql plus but I'm getting this error while executing this from visual studio
– portgas. D. Ace
2 days ago
2
2
On a general note, it's better to just run a query, or a view, or even to make a function that returns an object/rowtype, or a table of objects, or even a refcursor which you can open and read. Writing temporary data just for the sake of reading it on the other side should be the last fallback.
– GolezTrol
2 days ago
On a general note, it's better to just run a query, or a view, or even to make a function that returns an object/rowtype, or a table of objects, or even a refcursor which you can open and read. Writing temporary data just for the sake of reading it on the other side should be the last fallback.
– GolezTrol
2 days ago
Open your connection:
conn.Open(); before you execute any operations against the data store. Also it is not clear where conn resides. Is this a global (static) field somewhere? If so maybe there is a race condition where multiple threads are trying to open/close this connection at the same time (like a web application with multiple requests).– Igor
2 days ago
Open your connection:
conn.Open(); before you execute any operations against the data store. Also it is not clear where conn resides. Is this a global (static) field somewhere? If so maybe there is a race condition where multiple threads are trying to open/close this connection at the same time (like a web application with multiple requests).– Igor
2 days ago
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
If you set the type to stored procedure, all you need is the name of the stored procedure. So commandtext should be cv rather than exec cv.
-edit-
While the thing above is an issue as well, the screenshot you posted in your update suggests that the connection is not opened yet when you call ExecuteNonQuery.
Or maybe it is opened, but not assigned to the command. Since an application could theoretically have multiple connections, you have to tell the command which one to use. There is no global reference to the connection that the command will use implicitly. The example given in Oracle's documentation shows how this could be done through a property, although apparently you can also set it in the constructor, which you do for connections 1 and 3 but not for connection 2. I guess there is the problem. :o)
the connection is opened in the Db_connect function
– portgas. D. Ace
2 days ago
1
Yes, but look at the error. It says 'connection is closed'. This error is not in the stored proc, and maybe not even in the way you call the stored proc (apart from the thing I mentioned), but in the fact that your application (or your command or query to be precise) doesn't have a database connection.
– GolezTrol
2 days ago
no It's connected ,I checked it in data connections
– portgas. D. Ace
2 days ago
And is the connection assigned to the command? Looking at the code again, it seems you're not assigning it to comm2.
– GolezTrol
yesterday
add a comment |
up vote
0
down vote
Your approach is not really the "direct path", I would do it like this. Either select the table directly:
var s1 = "select email, collegename, cgpa, compname from student_cv
where (cgpa >= :x and yearsofexp >= :y) and compname = :z";
OracleCommand comm = new OracleCommand(s1, conn);
comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
comm.Parameters.Add("y", OracleType.Number).Value = comboBox2.Text;
comm.Parameters.Add("z", OracleType.VarChar2).Value = "some text";
OracleDataAdapter da = new OracleDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
Instead OracleDataAdapter and OracleDataAdapter you can also use OracleDataReader:
OracleDataReader dr = comm.ExecuteReader();
while ( dr.Read() )
// loop through the rows and process the rows one-by-one
dr.close();
The other way is to use a function or procedure like this:
create or replace FUNCTION cv (x in int, y in int, z in varchar2) RETURN SYS_REFCURSOR as
res SYS_REFCURSOR;
BEGIN
OPEN res FOR
select email, collegename, cgpa, compname
from student_cv
where (cgpa >= x and yearsofexp >= y) and compname = z;
RETURN res;
end;
And then call it in C# as this:
comm.CommandType = CommandType.Text;
comm.CommandText = "BEGIN :ret := cv(:x, :y, :z); END;";
comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
comm.Parameters.Add("y", OracleType.Number).Value = comboBox2.Text;
comm.Parameters.Add("z", OracleType.VarChar2).Value = "some text";
comm.Parameters.Add("ret", OracleDbType.RefCursor, ParameterDirection.ReturnValue);
OracleDataAdapter da = new OracleDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
Of course, you can also use OracleDataReader like above.
Note, I don't have a computer in front of me for any testing, so please forgive me if I did one or two typos in the code examples.
Regarding your actual error message, ensure the correct order:
- open the connection
- execute the command (or several commands)
- close the connection
As OracleConnection implements IDisposal you should enclose it by using like this
using (var conn = new OracleConnection() )
conn.ConnectionString = "Data Source=...";
conn.Open();
// Code from above
conn.Close();
OracleDataReader and OracleDataAdapter also implements IDisposal, so is should be also enclosed by using
add a comment |
up vote
-4
down vote
You may need a commit; before end; ?
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
If you set the type to stored procedure, all you need is the name of the stored procedure. So commandtext should be cv rather than exec cv.
-edit-
While the thing above is an issue as well, the screenshot you posted in your update suggests that the connection is not opened yet when you call ExecuteNonQuery.
Or maybe it is opened, but not assigned to the command. Since an application could theoretically have multiple connections, you have to tell the command which one to use. There is no global reference to the connection that the command will use implicitly. The example given in Oracle's documentation shows how this could be done through a property, although apparently you can also set it in the constructor, which you do for connections 1 and 3 but not for connection 2. I guess there is the problem. :o)
the connection is opened in the Db_connect function
– portgas. D. Ace
2 days ago
1
Yes, but look at the error. It says 'connection is closed'. This error is not in the stored proc, and maybe not even in the way you call the stored proc (apart from the thing I mentioned), but in the fact that your application (or your command or query to be precise) doesn't have a database connection.
– GolezTrol
2 days ago
no It's connected ,I checked it in data connections
– portgas. D. Ace
2 days ago
And is the connection assigned to the command? Looking at the code again, it seems you're not assigning it to comm2.
– GolezTrol
yesterday
add a comment |
up vote
1
down vote
If you set the type to stored procedure, all you need is the name of the stored procedure. So commandtext should be cv rather than exec cv.
-edit-
While the thing above is an issue as well, the screenshot you posted in your update suggests that the connection is not opened yet when you call ExecuteNonQuery.
Or maybe it is opened, but not assigned to the command. Since an application could theoretically have multiple connections, you have to tell the command which one to use. There is no global reference to the connection that the command will use implicitly. The example given in Oracle's documentation shows how this could be done through a property, although apparently you can also set it in the constructor, which you do for connections 1 and 3 but not for connection 2. I guess there is the problem. :o)
the connection is opened in the Db_connect function
– portgas. D. Ace
2 days ago
1
Yes, but look at the error. It says 'connection is closed'. This error is not in the stored proc, and maybe not even in the way you call the stored proc (apart from the thing I mentioned), but in the fact that your application (or your command or query to be precise) doesn't have a database connection.
– GolezTrol
2 days ago
no It's connected ,I checked it in data connections
– portgas. D. Ace
2 days ago
And is the connection assigned to the command? Looking at the code again, it seems you're not assigning it to comm2.
– GolezTrol
yesterday
add a comment |
up vote
1
down vote
up vote
1
down vote
If you set the type to stored procedure, all you need is the name of the stored procedure. So commandtext should be cv rather than exec cv.
-edit-
While the thing above is an issue as well, the screenshot you posted in your update suggests that the connection is not opened yet when you call ExecuteNonQuery.
Or maybe it is opened, but not assigned to the command. Since an application could theoretically have multiple connections, you have to tell the command which one to use. There is no global reference to the connection that the command will use implicitly. The example given in Oracle's documentation shows how this could be done through a property, although apparently you can also set it in the constructor, which you do for connections 1 and 3 but not for connection 2. I guess there is the problem. :o)
If you set the type to stored procedure, all you need is the name of the stored procedure. So commandtext should be cv rather than exec cv.
-edit-
While the thing above is an issue as well, the screenshot you posted in your update suggests that the connection is not opened yet when you call ExecuteNonQuery.
Or maybe it is opened, but not assigned to the command. Since an application could theoretically have multiple connections, you have to tell the command which one to use. There is no global reference to the connection that the command will use implicitly. The example given in Oracle's documentation shows how this could be done through a property, although apparently you can also set it in the constructor, which you do for connections 1 and 3 but not for connection 2. I guess there is the problem. :o)
edited yesterday
answered 2 days ago
GolezTrol
96.7k9127170
96.7k9127170
the connection is opened in the Db_connect function
– portgas. D. Ace
2 days ago
1
Yes, but look at the error. It says 'connection is closed'. This error is not in the stored proc, and maybe not even in the way you call the stored proc (apart from the thing I mentioned), but in the fact that your application (or your command or query to be precise) doesn't have a database connection.
– GolezTrol
2 days ago
no It's connected ,I checked it in data connections
– portgas. D. Ace
2 days ago
And is the connection assigned to the command? Looking at the code again, it seems you're not assigning it to comm2.
– GolezTrol
yesterday
add a comment |
the connection is opened in the Db_connect function
– portgas. D. Ace
2 days ago
1
Yes, but look at the error. It says 'connection is closed'. This error is not in the stored proc, and maybe not even in the way you call the stored proc (apart from the thing I mentioned), but in the fact that your application (or your command or query to be precise) doesn't have a database connection.
– GolezTrol
2 days ago
no It's connected ,I checked it in data connections
– portgas. D. Ace
2 days ago
And is the connection assigned to the command? Looking at the code again, it seems you're not assigning it to comm2.
– GolezTrol
yesterday
the connection is opened in the Db_connect function
– portgas. D. Ace
2 days ago
the connection is opened in the Db_connect function
– portgas. D. Ace
2 days ago
1
1
Yes, but look at the error. It says 'connection is closed'. This error is not in the stored proc, and maybe not even in the way you call the stored proc (apart from the thing I mentioned), but in the fact that your application (or your command or query to be precise) doesn't have a database connection.
– GolezTrol
2 days ago
Yes, but look at the error. It says 'connection is closed'. This error is not in the stored proc, and maybe not even in the way you call the stored proc (apart from the thing I mentioned), but in the fact that your application (or your command or query to be precise) doesn't have a database connection.
– GolezTrol
2 days ago
no It's connected ,I checked it in data connections
– portgas. D. Ace
2 days ago
no It's connected ,I checked it in data connections
– portgas. D. Ace
2 days ago
And is the connection assigned to the command? Looking at the code again, it seems you're not assigning it to comm2.
– GolezTrol
yesterday
And is the connection assigned to the command? Looking at the code again, it seems you're not assigning it to comm2.
– GolezTrol
yesterday
add a comment |
up vote
0
down vote
Your approach is not really the "direct path", I would do it like this. Either select the table directly:
var s1 = "select email, collegename, cgpa, compname from student_cv
where (cgpa >= :x and yearsofexp >= :y) and compname = :z";
OracleCommand comm = new OracleCommand(s1, conn);
comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
comm.Parameters.Add("y", OracleType.Number).Value = comboBox2.Text;
comm.Parameters.Add("z", OracleType.VarChar2).Value = "some text";
OracleDataAdapter da = new OracleDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
Instead OracleDataAdapter and OracleDataAdapter you can also use OracleDataReader:
OracleDataReader dr = comm.ExecuteReader();
while ( dr.Read() )
// loop through the rows and process the rows one-by-one
dr.close();
The other way is to use a function or procedure like this:
create or replace FUNCTION cv (x in int, y in int, z in varchar2) RETURN SYS_REFCURSOR as
res SYS_REFCURSOR;
BEGIN
OPEN res FOR
select email, collegename, cgpa, compname
from student_cv
where (cgpa >= x and yearsofexp >= y) and compname = z;
RETURN res;
end;
And then call it in C# as this:
comm.CommandType = CommandType.Text;
comm.CommandText = "BEGIN :ret := cv(:x, :y, :z); END;";
comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
comm.Parameters.Add("y", OracleType.Number).Value = comboBox2.Text;
comm.Parameters.Add("z", OracleType.VarChar2).Value = "some text";
comm.Parameters.Add("ret", OracleDbType.RefCursor, ParameterDirection.ReturnValue);
OracleDataAdapter da = new OracleDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
Of course, you can also use OracleDataReader like above.
Note, I don't have a computer in front of me for any testing, so please forgive me if I did one or two typos in the code examples.
Regarding your actual error message, ensure the correct order:
- open the connection
- execute the command (or several commands)
- close the connection
As OracleConnection implements IDisposal you should enclose it by using like this
using (var conn = new OracleConnection() )
conn.ConnectionString = "Data Source=...";
conn.Open();
// Code from above
conn.Close();
OracleDataReader and OracleDataAdapter also implements IDisposal, so is should be also enclosed by using
add a comment |
up vote
0
down vote
Your approach is not really the "direct path", I would do it like this. Either select the table directly:
var s1 = "select email, collegename, cgpa, compname from student_cv
where (cgpa >= :x and yearsofexp >= :y) and compname = :z";
OracleCommand comm = new OracleCommand(s1, conn);
comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
comm.Parameters.Add("y", OracleType.Number).Value = comboBox2.Text;
comm.Parameters.Add("z", OracleType.VarChar2).Value = "some text";
OracleDataAdapter da = new OracleDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
Instead OracleDataAdapter and OracleDataAdapter you can also use OracleDataReader:
OracleDataReader dr = comm.ExecuteReader();
while ( dr.Read() )
// loop through the rows and process the rows one-by-one
dr.close();
The other way is to use a function or procedure like this:
create or replace FUNCTION cv (x in int, y in int, z in varchar2) RETURN SYS_REFCURSOR as
res SYS_REFCURSOR;
BEGIN
OPEN res FOR
select email, collegename, cgpa, compname
from student_cv
where (cgpa >= x and yearsofexp >= y) and compname = z;
RETURN res;
end;
And then call it in C# as this:
comm.CommandType = CommandType.Text;
comm.CommandText = "BEGIN :ret := cv(:x, :y, :z); END;";
comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
comm.Parameters.Add("y", OracleType.Number).Value = comboBox2.Text;
comm.Parameters.Add("z", OracleType.VarChar2).Value = "some text";
comm.Parameters.Add("ret", OracleDbType.RefCursor, ParameterDirection.ReturnValue);
OracleDataAdapter da = new OracleDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
Of course, you can also use OracleDataReader like above.
Note, I don't have a computer in front of me for any testing, so please forgive me if I did one or two typos in the code examples.
Regarding your actual error message, ensure the correct order:
- open the connection
- execute the command (or several commands)
- close the connection
As OracleConnection implements IDisposal you should enclose it by using like this
using (var conn = new OracleConnection() )
conn.ConnectionString = "Data Source=...";
conn.Open();
// Code from above
conn.Close();
OracleDataReader and OracleDataAdapter also implements IDisposal, so is should be also enclosed by using
add a comment |
up vote
0
down vote
up vote
0
down vote
Your approach is not really the "direct path", I would do it like this. Either select the table directly:
var s1 = "select email, collegename, cgpa, compname from student_cv
where (cgpa >= :x and yearsofexp >= :y) and compname = :z";
OracleCommand comm = new OracleCommand(s1, conn);
comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
comm.Parameters.Add("y", OracleType.Number).Value = comboBox2.Text;
comm.Parameters.Add("z", OracleType.VarChar2).Value = "some text";
OracleDataAdapter da = new OracleDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
Instead OracleDataAdapter and OracleDataAdapter you can also use OracleDataReader:
OracleDataReader dr = comm.ExecuteReader();
while ( dr.Read() )
// loop through the rows and process the rows one-by-one
dr.close();
The other way is to use a function or procedure like this:
create or replace FUNCTION cv (x in int, y in int, z in varchar2) RETURN SYS_REFCURSOR as
res SYS_REFCURSOR;
BEGIN
OPEN res FOR
select email, collegename, cgpa, compname
from student_cv
where (cgpa >= x and yearsofexp >= y) and compname = z;
RETURN res;
end;
And then call it in C# as this:
comm.CommandType = CommandType.Text;
comm.CommandText = "BEGIN :ret := cv(:x, :y, :z); END;";
comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
comm.Parameters.Add("y", OracleType.Number).Value = comboBox2.Text;
comm.Parameters.Add("z", OracleType.VarChar2).Value = "some text";
comm.Parameters.Add("ret", OracleDbType.RefCursor, ParameterDirection.ReturnValue);
OracleDataAdapter da = new OracleDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
Of course, you can also use OracleDataReader like above.
Note, I don't have a computer in front of me for any testing, so please forgive me if I did one or two typos in the code examples.
Regarding your actual error message, ensure the correct order:
- open the connection
- execute the command (or several commands)
- close the connection
As OracleConnection implements IDisposal you should enclose it by using like this
using (var conn = new OracleConnection() )
conn.ConnectionString = "Data Source=...";
conn.Open();
// Code from above
conn.Close();
OracleDataReader and OracleDataAdapter also implements IDisposal, so is should be also enclosed by using
Your approach is not really the "direct path", I would do it like this. Either select the table directly:
var s1 = "select email, collegename, cgpa, compname from student_cv
where (cgpa >= :x and yearsofexp >= :y) and compname = :z";
OracleCommand comm = new OracleCommand(s1, conn);
comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
comm.Parameters.Add("y", OracleType.Number).Value = comboBox2.Text;
comm.Parameters.Add("z", OracleType.VarChar2).Value = "some text";
OracleDataAdapter da = new OracleDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
Instead OracleDataAdapter and OracleDataAdapter you can also use OracleDataReader:
OracleDataReader dr = comm.ExecuteReader();
while ( dr.Read() )
// loop through the rows and process the rows one-by-one
dr.close();
The other way is to use a function or procedure like this:
create or replace FUNCTION cv (x in int, y in int, z in varchar2) RETURN SYS_REFCURSOR as
res SYS_REFCURSOR;
BEGIN
OPEN res FOR
select email, collegename, cgpa, compname
from student_cv
where (cgpa >= x and yearsofexp >= y) and compname = z;
RETURN res;
end;
And then call it in C# as this:
comm.CommandType = CommandType.Text;
comm.CommandText = "BEGIN :ret := cv(:x, :y, :z); END;";
comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
comm.Parameters.Add("y", OracleType.Number).Value = comboBox2.Text;
comm.Parameters.Add("z", OracleType.VarChar2).Value = "some text";
comm.Parameters.Add("ret", OracleDbType.RefCursor, ParameterDirection.ReturnValue);
OracleDataAdapter da = new OracleDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
Of course, you can also use OracleDataReader like above.
Note, I don't have a computer in front of me for any testing, so please forgive me if I did one or two typos in the code examples.
Regarding your actual error message, ensure the correct order:
- open the connection
- execute the command (or several commands)
- close the connection
As OracleConnection implements IDisposal you should enclose it by using like this
using (var conn = new OracleConnection() )
conn.ConnectionString = "Data Source=...";
conn.Open();
// Code from above
conn.Close();
OracleDataReader and OracleDataAdapter also implements IDisposal, so is should be also enclosed by using
answered 2 days ago
Wernfried Domscheit
23k42757
23k42757
add a comment |
add a comment |
up vote
-4
down vote
You may need a commit; before end; ?
add a comment |
up vote
-4
down vote
You may need a commit; before end; ?
add a comment |
up vote
-4
down vote
up vote
-4
down vote
You may need a commit; before end; ?
You may need a commit; before end; ?
answered 2 days ago
Ted at ORCL.Pro
61018
61018
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229022%2fexecuting-procedure-from-visual-studio%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Have you tested that the procedure actually returns any values?
– stuartd
2 days ago
my procedure is working in sql plus but I'm getting this error while executing this from visual studio
– portgas. D. Ace
2 days ago
2
On a general note, it's better to just run a query, or a view, or even to make a function that returns an object/rowtype, or a table of objects, or even a refcursor which you can open and read. Writing temporary data just for the sake of reading it on the other side should be the last fallback.
– GolezTrol
2 days ago
Open your connection:
conn.Open();before you execute any operations against the data store. Also it is not clear whereconnresides. Is this a global (static) field somewhere? If so maybe there is a race condition where multiple threads are trying to open/close this connection at the same time (like a web application with multiple requests).– Igor
2 days ago