Hi all,
I want to check if a record exists in the DB.
I use two different ways to create the sql query.
One way is by using SqlCommand and SqlParameter.
The other way by building the query myself (without SqlParameter(s))
and using the SqlCommand to execute it.
In one I get false in the other i get true.
Can anyone help me?

////////////////////////////////////////////////
//Build the query using SqlParameters
////////////////////////////////////////////////
SqlCommand m_MessageSelectCommand = new SqlCommand();
m_MessageSelectCommand.CommandText =
"SELECT MessageNumber FROM Messages WHERE
CommunityNumber=@CommunityNumber and ForumParent=@ForumParent and
ForumNumber=@ForumNumber and WrittenBy=@WrittenBy and
MessageDate=@MessageDate";
m_MessageSelectCommand.Connection = m_Connection;
m_MessageSelectCommand.CommandTimeout = 0;
m_MessageSelectCommand.Parameters.Add(new
SqlParameter("@CommunityNumber", System.Data.SqlDbType.Int, 4,
"CommunityNumber"));
m_MessageSelectCommand.Parameters.Add(new SqlParameter("@ForumNumber",
System.Data.SqlDbType.Int, 4, "ForumNumber"));
m_MessageSelectCommand.Parameters.Add(new SqlParameter("@ForumParent",
System.Data.SqlDbType.Int, 4, "ForumParent"));
m_MessageSelectCommand.Parameters.Add(new SqlParameter("@WrittenBy",
System.Data.SqlDbType.NVarChar, 100, "WrittenBy"));
m_MessageSelectCommand.Parameters.Add(new SqlParameter("@MessageDate",
System.Data.SqlDbType.DateTime, 8, "MessageDate"));

// later in the code
m_MessageSelectCommand.Parameters["@CommunityNumber"].Value =
dlMessage.m_CommunityNumber;
m_MessageSelectCommand.Parameters["@ForumParent"].Value =
dlMessage.m_ForumNumber;
m_MessageSelectCommand.Parameters["@ForumNumber"].Value =
dlMessage.m_TopicNumber;
m_MessageSelectCommand.Parameters["@WrittenBy"].Value =
dlMessage.m_MessageAuthor;
m_MessageSelectCommand.Parameters["@MessageDate"].Value =
dlMessage.m_MessageDate;
SqlDataReader reader = m_MessageSelectCommand.ExecuteReader();
bool b = reader.HasRows;
// B IS FALSE!!!

////////////////////////////////////////////////
//The other way - just build the query by myself
////////////////////////////////////////////////
System.Text.StringBuilder sql = new System.Text.StringBuilder(256);
sql.Append("SELECT MessageNumber FROM Messages WHERE
CommunityNumber=");
sql.Append(dlMessage.m_CommunityNumber);
sql.Append(" and ForumParent=");
sql.Append(dlMessage.m_ForumNumber);
sql.Append(" and ForumNumber=");
sql.Append(dlMessage.m_TopicNumber);
sql.Append(" and WrittenBy='");
sql.Append(dlMessage.m_MessageAuthor);
sql.Append("' and MessageDate='");
sql.Append(dlMessage.m_MessageDate);
sql.Append("'");
SqlCommand c = new SqlCommand(sql.ToString(), m_Connection);
SqlDataReader reader = c.ExecuteReader();
bool b = reader.HasRows;
// I get that b is true.
This happens durring the same program execution
How can this be???

RE: Different results with the same query?!? by bruce

bruce
Thu Dec 30 08:29:02 CST 2004

I can't tell for sure because some information is missing, but my guess is
that your problem has to do with the MessageDate parameter. When you pass a
Date value into a parameter, I believe that it will be 'converted' to a
dd/mm/yy hh:mm:ss format. Since your date comparison is =, it must be an
exact match and it might not be. Just to confirm this, change the
'MessageDate =' portion of the statement to 'DateDiff(d, MessageDate,
@MessageDate) = 0'. I'm guessing that will give you the same results in both
cases.

Hope that helps.

Bruce Johnson [.NET MVP]
http://www.objectsharp.com/blogs/bruce

"trendum" wrote:

> Hi all,
> I want to check if a record exists in the DB.
> I use two different ways to create the sql query.
> One way is by using SqlCommand and SqlParameter.
> The other way by building the query myself (without SqlParameter(s))
> and using the SqlCommand to execute it.
> In one I get false in the other i get true.
> Can anyone help me?
>
> ////////////////////////////////////////////////
> //Build the query using SqlParameters
> ////////////////////////////////////////////////
> SqlCommand m_MessageSelectCommand = new SqlCommand();
> m_MessageSelectCommand.CommandText =
> "SELECT MessageNumber FROM Messages WHERE
> CommunityNumber=@CommunityNumber and ForumParent=@ForumParent and
> ForumNumber=@ForumNumber and WrittenBy=@WrittenBy and
> MessageDate=@MessageDate";
> m_MessageSelectCommand.Connection = m_Connection;
> m_MessageSelectCommand.CommandTimeout = 0;
> m_MessageSelectCommand.Parameters.Add(new
> SqlParameter("@CommunityNumber", System.Data.SqlDbType.Int, 4,
> "CommunityNumber"));
> m_MessageSelectCommand.Parameters.Add(new SqlParameter("@ForumNumber",
> System.Data.SqlDbType.Int, 4, "ForumNumber"));
> m_MessageSelectCommand.Parameters.Add(new SqlParameter("@ForumParent",
> System.Data.SqlDbType.Int, 4, "ForumParent"));
> m_MessageSelectCommand.Parameters.Add(new SqlParameter("@WrittenBy",
> System.Data.SqlDbType.NVarChar, 100, "WrittenBy"));
> m_MessageSelectCommand.Parameters.Add(new SqlParameter("@MessageDate",
> System.Data.SqlDbType.DateTime, 8, "MessageDate"));
>
> // later in the code
> m_MessageSelectCommand.Parameters["@CommunityNumber"].Value =
> dlMessage.m_CommunityNumber;
> m_MessageSelectCommand.Parameters["@ForumParent"].Value =
> dlMessage.m_ForumNumber;
> m_MessageSelectCommand.Parameters["@ForumNumber"].Value =
> dlMessage.m_TopicNumber;
> m_MessageSelectCommand.Parameters["@WrittenBy"].Value =
> dlMessage.m_MessageAuthor;
> m_MessageSelectCommand.Parameters["@MessageDate"].Value =
> dlMessage.m_MessageDate;
> SqlDataReader reader = m_MessageSelectCommand.ExecuteReader();
> bool b = reader.HasRows;
> // B IS FALSE!!!
>
> ////////////////////////////////////////////////
> //The other way - just build the query by myself
> ////////////////////////////////////////////////
> System.Text.StringBuilder sql = new System.Text.StringBuilder(256);
> sql.Append("SELECT MessageNumber FROM Messages WHERE
> CommunityNumber=");
> sql.Append(dlMessage.m_CommunityNumber);
> sql.Append(" and ForumParent=");
> sql.Append(dlMessage.m_ForumNumber);
> sql.Append(" and ForumNumber=");
> sql.Append(dlMessage.m_TopicNumber);
> sql.Append(" and WrittenBy='");
> sql.Append(dlMessage.m_MessageAuthor);
> sql.Append("' and MessageDate='");
> sql.Append(dlMessage.m_MessageDate);
> sql.Append("'");
> SqlCommand c = new SqlCommand(sql.ToString(), m_Connection);
> SqlDataReader reader = c.ExecuteReader();
> bool b = reader.HasRows;
> // I get that b is true.
> This happens durring the same program execution
> How can this be???
>
>