MySQL Connector: Parameters Not Added

In my query log for MySQL, I see that my parameters are not being added. Here is my code:

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();
                }

Looking at the query log, I see the following:

SELECT COUNT(*) AS totalViews
                                                      FROM pr_postreleaseviewslog AS prvl
                                                      WHERE prvl.dateCreated BETWEEN (@startDate) AND (@endDate) AND prvl.postreleaseID IN (1,2)

I used the MySQL.NET connector for countless projects (I have a base class that takes care of opening these connections and closing transactions, etc.). However, I have accepted this application, and here I am now.

Thanks for the help!

+3
source share
2 answers

Try it.

mySqlCommand.Parameters.Add("@CustomerID", SqlDbType.NChar, 5);
mySqlCommand.Parameters["@CustomerID"].Value = "T1COM";
+2
source

Some SQL clients, especially for MySql, use "?" instead of "@".

0
source

Source: https://habr.com/ru/post/1717455/


All Articles