查看MySQL的查询日志,我看到我的参数没有被添加。这是我的代码:
MySqlConnection conn = new MySqlConnection(ApplicationVariables.ConnectionString());
MySqlCommand com = new MySqlCommand();
try
{
conn.Open();
com.Connection = conn;
com.CommandText = String.Format(@"SELECT COUNT(*) AS totalViews
FROM pr_postreleaseviewslog AS prvl
WHERE prvl.dateCreated BETWEEN (@startDate) AND (@endDate) AND prvl.postreleaseID IN ({0})"
, ids);
com.CommandType = CommandType.Text;
com.Parameters.Add(new MySqlParameter("@startDate", thisCampaign.Startdate));
com.Parameters.Add(new MySqlParameter("@endDate", endDate));
numViews = Convert.ToInt32(com.ExecuteScalar());
}
catch (Exception ex)
{
}
finally
{
conn.Dispose();
com.Dispose();
}
查看查询日志,我看到了:
SELECT COUNT(*) AS totalViews
FROM pr_postreleaseviewslog AS prvl
WHERE prvl.dateCreated BETWEEN (@startDate) AND (@endDate) AND prvl.postreleaseID IN (1,2)
我在无数项目中使用了MySQL .NET连接器(实际上我有一个基类来负责打开这些连接,并用事务关闭它们等)。但是,我接手了这个应用程序,现在我在这里。
感谢您的帮助!
答案 0 :(得分:2)
试试这样。
mySqlCommand.Parameters.Add("@CustomerID", SqlDbType.NChar, 5);
mySqlCommand.Parameters["@CustomerID"].Value = "T1COM";
答案 1 :(得分:0)
某些SQL客户端,特别是对于MySql使用“?”而不是“@”。