user3685232
user3685232

Reputation: 3

MySQL select query with parameter

I am trying to use MYSQL select query with c#.

Following query for searching "ID" is working fine:

conn = new MySqlConnection(cs);
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText = "select * from catalog_product_entity where entity_id = ?Id";
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
cmd.Parameters.Add("?Id", SqlDbType.Text).Value = ProductList[i].ProductId.ToString();
adp.Fill(MagentoProduct);

Now, I want to search exact string value in table. I am using following code and its giving empty result:

My Code:

                            conn = new MySqlConnection(cs);
                            conn.Open();
                            cmd = new MySqlCommand("select * from catalog_category_entity_varchar where value = @Value;", conn);
                            cmd.Parameters.AddWithValue("@Value", "Storybooks");
                            MySqlDataReader r = cmd.ExecuteReader();

                            while (r.Read())
                            {
                                log.WriteEntry(r.GetString("value"));
                            }

Upvotes: 0

Views: 5506

Answers (2)

BlaugranaGent
BlaugranaGent

Reputation: 100

You could try SQL Reader

c = new MySqlCommand("select * from catalog_product_entity where column_nam = @Value;", conn);
c.Parameters.AddWithValue("@Value", your string);
MySqlDataReader r = c.ExecuteReader();

and then use Reader methods like reader.GetString("column_name"), ....

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499740

This is the problem:

where value = '?cname'

That's specifying ?cname as the literal value you're searching for - when you actually just want the parameter. Remove the quotes and it should be fine:

where value = ?cname

(You should use using statements for the connection and command, mind you...)

Upvotes: 3

Related Questions