When I try to run a package on our oracle database from Oracle's .net provider, or the Microsoft oracle provider, it gives me the following error:
{"ORA-06550: line 1, column 7:\nPLS-00221: 'BEGIN_TRANSACTION' is not a procedure or is undefined\nORA-06550: line 1, column 7:\nPL/SQL: Statement ignored"}
Here is my C# code:
OracleConnection cn = new OracleConnection(connectionString);
OracleCommand cmd = new OracleCommand();
cmd.Connection = cn;
cmd.CommandText = "DOTNET.SYSTEM_CRUD.BEGIN_TRANSACTION";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("p_user_id", OracleDbType.Decimal, ParameterDirection.Input).Value = 4720;
cmd.Parameters.Add("p_commt", OracleDbType.Varchar2, ParameterDirection.Input).Value = "TEST";
//cmd.Parameters.Add("return_value", OracleDbType.Decimal).Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
Here is my package definition:
Function Begin_Transaction ( p_user_id IN NUMBER,
p_commt IN VARCHAR2
)
RETURN Number;
When I uncomment the third parameter that I've added, I get a different error:
{"ORA-06502: PL/SQL: numeric or value error: character to number conversion error\nORA-06512: at line 1"}
Try following code:
using (OracleCommand cmd = new OracleCommand())
{
cmd.Connection = cn;
cmd.CommandText = "DOTNET.SYSTEM_CRUD.BEGIN_TRANSACTION";
cmd.CommandType = CommandType.StoredProcedure;
cmd.BindByName = true;
cmd.Parameters.Add(new OracleParameter("p_user_id", OracleDbType.Decimal, 4720, ParameterDirection.Input));
cmd.Parameters.Add(new OracleParameter("p_commt", OracleDbType.Varchar2, "TEST", ParameterDirection.Input));
cmd.ExecuteNonQuery();
}
Comment:
Don't forgive wrap disposable objects in using construction.
I removed the username from the package definition, and also uncommented my add parameter for the return value.
using (OracleConnection cn = new OracleConnection(connectionString))
{
using (OracleCommand cmd = new OracleCommand())
{
cmd.Connection = cn;
cmd.CommandText = "SYSTEM_CRUD.BEGIN_TRANSACTION";
cmd.CommandType = CommandType.StoredProcedure;
cmd.BindByName = true;
cmd.Parameters.Add("p_user_id", OracleDbType.Decimal, ParameterDirection.Input).Value = 4720;
cmd.Parameters.Add("p_commt", OracleDbType.Varchar2, ParameterDirection.Input).Value = "TEST";
cmd.Parameters.Add("return_value", OracleDbType.Decimal).Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
Related
I'm using the flowing code to call the oracle function with the return value,
but it returns null always
OracleCommand cmd = new OracleCommand();
using (OracleConnection cnn = new OracleConnection("Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=321352427544)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=test)));User ID=abc;Password=123;"))
{
cmd.Connection = cnn;
cmd.CommandText = "GetEmp";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("P_EMP_ID", OracleDbType.Int32).Value = 4241;
cmd.Parameters.Add(new OracleParameter("return_value", OracleDbType.Int32)).Direction = ParameterDirection.ReturnValue;
cnn.Open();
cmd.ExecuteNonQuery();
string Count = (string)cmd.Parameters["return_value"].Value;
cnn.Close();
}
the problem was solved in two ways by
add cmd.BindByName = true;
add the return parameter in the first: cmd.Parameters.Add(new OracleParameter("return_value", OracleDbType.Int32)).Direction = ParameterDirection.ReturnValue;
Can anybody tell me what am I doing wrong here? I am facing Oracle.DataAccess.Client.OracleException exception and when I clicked to view details I saw
ORA-06550: line 1, column 7: PLS-00306: wrong number or types of
arguments in call to 'PRC_ABCD_GETALL' ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
I have a store procedure like this
PROCEDURE PRC_ABCD_GETALL (resultset_out OUT TYPES.cursorType)
AS
BEGIN
OPEN
resultset_out FOR SELECT * FROM ABCD;
END PRC_ABCD_GETALL;
And my C# code is like this
using (OracleConnection conn = new OracleConnection(cnn))
{
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "PRC_ABCD_GETALL";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("resultset_out", OracleDbType.RefCursor,
ParameterDirection.Output);
OracleDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
result.Add(Construct(rdr));
}
}
you must use sys_refcursor instead TYPES.cursorType in your procedure's declaration.
Types.cursorType isn't the same.
You can try changing the c# invocation data type instead of changing it on store procedure:
using (OracleConnection conn = new OracleConnection(cnn)){
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "PRC_ABCD_GETALL";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("resultset_out", OracleType.Cursor,
ParameterDirection.Output);
OracleDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
result.Add(Construct(rdr));
}}
I am using mySQL with .net 4.5 to do typical CRUD commands.
I have an issue wit a parameter which is called 'Message' (Chat application). It seems the text inserted into the database is being cut off at 200 bytes.
I have tried multiple data types for the column. Currently it is TEXT, I have tried LONGTEXT and varchar(1000) etc; but still, it gets cut off at 200 bytes.
What is going on?
MySqlConnection conn = new MySqlConnection(ConnectionString());
conn.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.Parameters.Clear();
cmd.CommandText = "createStaffMessage";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#sessionID", sessionID);
cmd.Parameters["#sessionID"].Direction = ParameterDirection.Input;
cmd.Parameters.AddWithValue("#chatID", chatId);
cmd.Parameters["#chatID"].Direction = ParameterDirection.Input;
cmd.Parameters.AddWithValue("#message", message);
cmd.Parameters["#message"].Direction = ParameterDirection.Input;
cmd.Parameters.AddWithValue("#byStaff", byStaff);
cmd.Parameters["#byStaff"].Direction = ParameterDirection.Input;
cmd.Parameters.AddWithValue("#staffId", staffId);
cmd.Parameters["#staffId"].Direction = ParameterDirection.Input;
cmd.Parameters.AddWithValue("#dateTimeString", DateTime.Now.ToLongTimeString() + " (NZ Time)");
cmd.Parameters["#dateTimeString"].Direction = ParameterDirection.Input;
cmd.Connection = conn;
MySqlDataReader rdr = cmd.ExecuteReader();
rdr.Read();
string[] newMessageDetails = new string[2];
newMessageDetails[0] = rdr[0].ToString();
newMessageDetails[1] = rdr[1].ToString();
conn.Close();
return newMessageDetails;
I'm trying to insert some data read from XML files into my database. The problem is that it only executes one prepared statement (the first one i defined) and this causes errors.
This works:
conn = getConnection();
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO ra_gestiuni ([denumire],[id_ra]) Values(#denumire,#id_ra)";
SqlCeParameter numeParam = new SqlCeParameter("#denumire", SqlDbType.NVarChar, 100);
numeParam.Value = denumire;
cmd.Parameters.Add(numeParam);
SqlCeParameter idRAParam = new SqlCeParameter("#id_ra", SqlDbType.Int);
idRAParam.Value = idRA;
cmd.Parameters.Add(idRAParam);
cmd.Prepare();
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
This does not work anymore:
conn = getConnection();
conn.Open();
SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO ra_active ([denumire],[gestiune],[utilizator],[tip_activ],[nr_invetar],[categorie_activ],[patrimoniu],[id_ra]) Values(#denumire,#gestiune,#utilizator,#tip_activ,#nr_invetar,#categorie_activ,#patrimoniu,#id_ra)";
SqlCeParameter numeParam = new SqlCeParameter("#denumire", SqlDbType.NVarChar, 100);
numeParam.Value = denumire;
cmd.Parameters.Add(numeParam);
SqlCeParameter gestiuneParam = new SqlCeParameter("#gestiune", SqlDbType.Int);
gestiuneParam.Value = gestiune;
cmd.Parameters.Add(gestiuneParam);
SqlCeParameter utilizatorParam = new SqlCeParameter("#utilizator", SqlDbType.Int);
utilizatorParam.Value = utilizator;
cmd.Parameters.Add(utilizatorParam);
SqlCeParameter nrInventarParam = new SqlCeParameter("#nr_inventar", SqlDbType.NVarChar);
nrInventarParam.Value = nrInventar;
cmd.Parameters.Add(nrInventarParam);
SqlCeParameter categorieActivParam = new SqlCeParameter("#categorie_activ", SqlDbType.NVarChar);
categorieActivParam.Value = categorieActiv;
cmd.Parameters.Add(categorieActivParam);
SqlCeParameter patrimoniuParam = new SqlCeParameter("#patrimoniu", SqlDbType.Int);
patrimoniuParam.Value = patrimoniu;
cmd.Parameters.Add(patrimoniuParam);
SqlCeParameter tipActivParam = new SqlCeParameter("#tip_activ", SqlDbType.Int);
tipActivParam.Value = tipActiv;
cmd.Parameters.Add(tipActivParam);
SqlCeParameter idRAParam = new SqlCeParameter("#id_ra", SqlDbType.Int);
idRAParam.Value = idRA;
cmd.Parameters.Add(idRAParam);
cmd.Prepare();
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
I get this exception:
The column name is not valid [ Node name (if any) = ,Column name = gestiune ]
This happens because it tries to insert in the previous table (from the previous prepared statement). This is insane, I haven't found any solution to this.
What I see is the parameter #nr_inventar does not exists in your command text (you have #nr_invetar there). Also if you really need to prepare the command before executing, you should set a size for all nvarchar parameters, like you did for #denumire.
I have a stored procedure with an output parameter. How do I read this value using C# code?
I assume you use ADO.NET? If so, the SqlParameter class has the property "Direction". Set direction to output and after the query has executed you read the value from that parameter.
Something like this:
using (SqlCommand cmd = new SqlCommand("MyStoredProcedure", cn))
{
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parm = new SqlParameter("#pkid", SqlDbType.Int);
parm.Value = 1;
parm.Direction = ParameterDirection.Input;
cmd.Parameters.Add(parm);
SqlParameter parm2 = new SqlParameter("#ProductName", SqlDbType.VarChar);
parm2.Size = 50;
parm2.Direction = ParameterDirection.Output; // This is important!
cmd.Parameters.Add(parm2);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}