T
trendum
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???
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???