Oracle update command not working - c#

I tried below code,
ora.Open();
OracleCommand cmd = new OracleCommand() ;
cmd.Connection= ora;
cmd.CommandTimeout = 0;
cmd.CommandText = "update test set name= :name";
OracleParameter name = new OracleParameter("name", OracleDbType.Varchar2);
cmd.BindByName = true;
name.Value = "test";
cmd.Parameters.Add(a);
cmd.ExecuteNonQuery();
ExecuteNonQuery does not work and no errors. What's wrong?
I work with Visual Studio 2012 and Oracle Express 11g

You are adding a parameter named a instead the one you've created. Try this:
ora.Open();
using (OracleCommand cmd = new OracleCommand())
{
cmd.Connection = ora;
cmd.BindByName = true;
cmd.CommandText = "update test set name = :name";
OracleParameter name = new OracleParameter("name", OracleDbType.Varchar2);
name.Value = "test";
cmd.Parameters.Add(name); // add the name parameter, not "a" object.
cmd.ExecuteNonQuery();
}

Maybe you are missing the Prepare function?
try adding
cmd.Prepare(); just after cmd.Parameters.Add(name);

Related

Running Oracle Package: ORA-06550 || ORA-06502

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

Oracle connection and running script in c#

I have got the following code
using (OracleConnection con = new OracleConnection())
{
con.ConnectionString = My_connection_string;
FileInfo file = new FileInfo(Server.MapPath("~/Scripts/call_proc.sql"));
string script = file.OpenText().ReadToEnd();
con.Open();
OracleCommand cmd = new OracleCommand(script, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.BindByName = true;
cmd.ExecuteNonQuery();
}
The call_proc code is
//returns Hello
set serveroutput on;
begin
test.tmp_test();
end;
When I try to run the above code it gives me the following errors.
ORA-06550: line 1, column 11: PL/SQL: ORA-00922: missing or invalid
option ORA-06550: line 1, column 7:
You could just use a OracleCommand with the procedure name For sample
Try something like this:
using (OracleConnection con = new OracleConnection())
{
con.ConnectionString = My_connection_string;
con.Open();
OracleCommand cmd = new OracleCommand("temp.tmp_test", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.BindByName = true;
var result = cmd.ExecuteScalar();
if (result != null)
{
string stringResult = result.ToString();
// show result here
}
}
You can use command.ExecuteScalar() if you want to take the first column and first line, it will return an object instance.
You can also use a Oracle Data Reader from command.ExecuteDataReader() if you want to read many lines and columns (very common when you want to manipulate an output from a select query).

MySqlConnection Parameters cutting off data at 200bytes

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;

SQL Server CE executes the same prepared statement no matter what

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.

How to run the stored procedure that has OUTPUT parameter from C#?

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

Categories

Resources