Search code examples
c#oracleparametersoraclecommand

How to pass parameters in a oracle SQL query?


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

Solution

  • 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;
        }