I have a funky problem.. I have this code:
using (MySqlCommand cmd = new MySqlCommand())
{
cmd.Connection = this.connection;
cmd.CommandText = "SELECT id FROM links WHERE url LIKE '@url'";
cmd.Parameters.AddWithValue("@url", parent_url.Trim());
cmd.Prepare();
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
parent_id = reader.GetInt32(0);
}
}
}
What my problem is that.. sometimes it doesn't return a value when there is a an answer in the database. That's very strange. Also I have noticed that some items in the database have a space behind it so this is why I made this:
using (MySqlCommand cmd = new MySqlCommand())
{
cmd.Connection = this.connection;
cmd.CommandText = "SELECT id FROM links WHERE url LIKE '@url'";
cmd.Parameters.AddWithValue("@url", parent_url.Trim());
cmd.Prepare();
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
parent_id = reader.GetInt32(0);
}
}
}
if(parent_id == 0)
{
using (MySqlCommand cmd = new MySqlCommand())
{
cmd.Connection = this.connection;
cmd.CommandText = "SELECT id FROM links WHERE url LIKE '@url '";
cmd.Parameters.AddWithValue("@url", parent_url.Trim());
cmd.Prepare();
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
parent_id = reader.GetInt32(0);
}
}
}
}
But it still doesn't help me some times.. even if the item doesn't have a space after it. So yea.. can you guys please give me some advice.. what should I try.
"SELECT id FROM links WHERE url LIKE @url";
remove the
''
from the parameter then only it will consider as parameter
otherwise it will be only a string value equal to
@url
and also if you need to search given string on database column use
%
like belowcmd.Parameters.AddWithValue("@url", "%" + parent_url.Trim()+ "%");
UPDATE:
you can use trim function as below
"SELECT id FROM links WHERE trim(url) LIKE @url";
0 comments:
Post a Comment