How to pass parameters in a oracle SQL query?

5.1k Views Asked by At

For the past few days I cannot pass any fixed parameters to my SQL query. I try all possible tutorials to pass the parameter to a query, but nothing works.

However, I have seen by putting fixed parameters directly in the query (method 1) it worked perfectly.

I do not see that in method 2 prevents the functioning of my function.

When I say that it does not work is that in the first method my reader is filled while in the method 2 my reader is empty

method 1 : works (i don't need this kind of function)

public void VerifierVersionDejaPresnte(ParseurXML.DonneesGlobales donneGlobale)
{
    OracleCommand cmd = new OracleCommand();
    cmd.Connection = conn;
    cmd.CommandText = "select nom_projet from analyses where nom_projet='demonstration'";
    cmd.CommandType = CommandType.Text;
    OracleDataReader dr = cmd.ExecuteReader();
    if (dr.Read())
        Console.WriteLine("Data already exist");
    else
        Console.WriteLine("Data doesn't exist");
}  

method 2 : doesn't works (I need this kind of function)

public void VerifierVersionDejaPresnte(ParseurXML.DonneesGlobales donneGlobale)
{
    OracleCommand cmd = new OracleCommand();
    cmd.Connection = conn;
    cmd.CommandText = "select nom_projet from analyses where nom_projet=:test";
    cmd.Parameters.Add(new OracleParameter("test", "demonstration"));
    cmd.CommandType = CommandType.Text;
    OracleDataReader dr = cmd.ExecuteReader();
    if (dr.Read())
        Console.WriteLine("Data already exist");
    else
        Console.WriteLine("Data doesn't exist");
}
3

There are 3 best solutions below

0
On BEST ANSWER

After many hours of research, i have I finally found the solution:

  public Boolean VerifierVersionDejaPresnte(ParseurXML.DonneesGlobales donneGlobale)
    {
        string str = "demonstration";
        OracleCommand cmd = new OracleCommand();
        cmd.Connection = conn;
        cmd.BindByName = true;
        cmd.CommandText = "select * from analyses where nom_projet='"+str+"'";
        cmd.CommandType = CommandType.Text;
        OracleDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            Console.WriteLine("Data already exist");
            return true;
        }
        Console.WriteLine("Data doesn't already exist");
        return true;
    }
0
On
String updateCmd;
        SqlCommand myCommandUpd;

updateCmd = "UPDATE RHRMVacationRequest SET [EmplId] = @Emplid WHERE RecId = @RecId";

        myCommandUpd = new SqlCommand(updateCmd, DataBase.GetConnetionToBase());
        myCommandUpd.Parameters.Add(new SqlParameter("@Emplid", SqlDbType.VarChar, 10));
        myCommandUpd.Parameters.Add(new SqlParameter("@RecId", SqlDbType.BigInt));

        myCommandUpd.Parameters["@Emplid"].Value = emplIdUpd.Text.Trim();
        myCommandUpd.Parameters["@RecId"].Value = Convert.ToInt64(RecIdUpd.Value.Trim());
        myCommandUpd.Connection.Open();
        myCommandUpd.ExecuteNonQuery();
3
On

may be i recall the wrong way round - maybe i tried :? and it did not work and used :test - but i think i got you issue, check the code below.

public void VerifierVersionDejaPresnte(ParseurXML.DonneesGlobales donneGlobale)
{
    OracleCommand cmd = new OracleCommand();
    cmd.Connection = conn;
    cmd.CommandText = "select nom_projet from analyses where nom_projet=:test";
    cmd.Parameters.Add(new OracleParameter(":test", "demonstration"));
    cmd.CommandType = CommandType.Text;
    OracleDataReader dr = cmd.ExecuteReader();
    if (dr.Read())
        Console.WriteLine("Data already exist");
    else
        Console.WriteLine("Data doesn't exist");
}