mardi 4 août 2015

Add parameters in query on c#

In my code behind of aspx page I have problem to passed values on Parameters in sql query.

Step 1:

I add in List the output of an query:

while (reader.Read())
{
    idcolor = reader["idcolor"].ToString();
    colorList.Add(idcolor.ToString());
}

ns = string.Join("','", colorList.ToArray());

In debug the output is:

ns = red','green

Step 2:

I need use the values of string ns on a sql query.

And pass the values of string ns in parameters:

str = null;
str = ns == null ? "" : ns.ToString();

sql = @" SELECT * FROM Experience WHERE Colors IN (?); ";

    DataSet dsColors = new DataSet();

    using (OdbcConnection cn =
      new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
    {
        cn.Open();

        using (OdbcCommand cmd = new OdbcCommand(sql, cn))
        {

            cmd.Parameters.AddWithValue("param1", Server.UrlDecode(str.ToString()));

            OdbcDataAdapter adapter = new OdbcDataAdapter(cmd);
            adapter.Fill(dsColors);
        }
    }

    return dsColors;

Step 3:

If used in query :

sql = @" SELECT * FROM Experience WHERE Colors IN (?); ";

The output in dataset is empty.

If used in query :

sql = @" SELECT * FROM Experience WHERE Colors IN ( '" + Server.UrlDecode(str.ToString()) + "' ); ";

The output in dataset is right.

Anybody know how can I resolve do this?

Can you suggest?

Can you help me?

Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire