You are using Microsoft Oracle Client (using System.Data.OracleClient
). Your life would be a bit easier if you used appropriate version of Oracle Data Provider for .NET of Oracle Data Access Components(ODAC) - you can take advantage of native oracle data types and features. Besides, try to use Oracle (Not ANSI data types, which will be converted to Oracle native data types anyway) data types. Use number
instead of int
for instance. Moreover, use varchar2()
data type instead of varchar
. Although varchar2
and varchar
are synonyms as of now, behavior of varchar
may change in the future, so better stick to varchar2
.
Here is an example:
Set-up:
/* your table */
create table forum(
course_id number,
question varchar2(123),
postername varchar2(123),
blog_date date
);
/* the stored procedure*/
create or replace procedure InsertForum(
p_course_id in number,
p_question in varchar2,
p_postername in varchar2,
p_blog_date in date
) as
begin
insert into forum (course_id, question, postername, blog_date)
values (p_course_id, p_question, p_postername, p_blog_date);
commit;
/* As a side note. It would be better to allow a calling application
to commit or rollback transaction.
*/
end;
C# code:
...
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
....
string oraConnectionString = "Data source=data_source;User id=user;password=pwd;";
OracleConnection oraConnection = new OracleConnection(oraConnectionString);
oraConnection.Open();
OracleCommand oraCmd = new OracleCommand("InsertForum", oraConnection);
oraCmd.CommandType = System.Data.CommandType.StoredProcedure;
/*or Decimal*/
oraCmd.Parameters.Add(new OracleParameter("p_course_id", OracleDbType.Int32,
System.Data.ParameterDirection.Input)).Value = 123;
oraCmd.Parameters.Add(new OracleParameter("p_question", OracleDbType.Varchar2,
System.Data.ParameterDirection.Input)).Value = "question";
oraCmd.Parameters.Add(new OracleParameter("p_postername", OracleDbType.Varchar2,
System.Data.ParameterDirection.Input)).Value = "postername";
oraCmd.Parameters.Add(new OracleParameter("p_blog_date", OracleDbType.Date,
System.Data.ParameterDirection.Input)).Value = DateTime.Now;
try
{
oraCmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Result:
column question format a11;
column postername format a11;
select *
from forum;
COURSE_ID QUESTION POSTERNAME BLOG_DATE
---------- ----------- ----------- -------------------
123 question postername 02-03-2014 12:11:59
It also should be noted that when you execute a stored procedure by calling ExecuteNonQuery()
method, the number of affected rows will be -1. It will give you the expected number of affected rows only if you execute a DML(insert
, etc.) statement .