C# MySqlCommand似乎没有传递参数

C# MySqlCommand似乎没有传递参数,c#,mysql,C#,Mysql,下面的代码似乎没有将参数值插入查询(查询没有返回任何内容)。如果我在DB中测试该查询(当然不是?author参数),我会键入传递的值并返回一些行。为什么 var conn = new MySqlConnection(connectionString); MySqlCommand comm = new MySqlCommand("", conn); comm.Parameters.Add(new MySqlParameter("?author", author)); //I'

下面的代码似乎没有将参数值插入查询(查询没有返回任何内容)。如果我在DB中测试该查询(当然不是?author参数),我会键入传递的值并返回一些行。为什么

   var conn = new MySqlConnection(connectionString);

   MySqlCommand comm = new MySqlCommand("", conn);

   comm.Parameters.Add(new MySqlParameter("?author", author)); //I've also tried AddWithValue method

   comm.CommandText = .....;

            conn.Open();
            MySqlDataReader myReader = comm.ExecuteReader();

            try
            {
                while (myReader.Read())
                {
                    //unreachable code because nothing is returned
                }
            }
            catch
            {
                myReader.Close();
                conn.Close();
                categoriesList.Clear();
            }
            finally
            {
                myReader.Close();
                conn.Close();
            }

它可能很简单,因为.Parameters.Add语句前面需要.CommandText。因此:

   comm.CommandText = .....;
   comm.Parameters.Add(new MySqlParameter("?author", author)); //I've also tried AddWithValue method

它可能很简单,因为.Parameters.Add语句前面需要.CommandText。因此:

   comm.CommandText = .....;
   comm.Parameters.Add(new MySqlParameter("?author", author)); //I've also tried AddWithValue method

由于您使用的是
MySQLCommand
,因此不要在参数名称中添加

如果您有这样的查询:

comm.CommandText = "INSERT INTO tableName(colA) VALUES (@param1)";
comm.CommandType = CommandType.Text;
那么你必须这样做:

comm.Parameters.AddWithValue("param1", "yourValue");
更新

假设您有类似于条件的查询:

"SELECT * FROM WHERE colA Like '%value%'"
然后试试这个:

comm.CommandText = "SELECT * FROM WHERE colA Like CONCAT('%', @param1, '%'");
comm.Parameters.AddWithValue("param1", "yourValue");

由于您使用的是
MySQLCommand
,因此不要在参数名称中添加

如果您有这样的查询:

comm.CommandText = "INSERT INTO tableName(colA) VALUES (@param1)";
comm.CommandType = CommandType.Text;
那么你必须这样做:

comm.Parameters.AddWithValue("param1", "yourValue");
更新

假设您有类似于条件的查询:

"SELECT * FROM WHERE colA Like '%value%'"
然后试试这个:

comm.CommandText = "SELECT * FROM WHERE colA Like CONCAT('%', @param1, '%'");
comm.Parameters.AddWithValue("param1", "yourValue");

sql代码段:
和b.AuthorNames类似“%@author%”
然后
comm.Parameters.AddWithValue(“author”,author”);
然后仍然nothing@user1215114您应该使用
CONCAT
函数。我的答案现在已更新。sql代码段:
和b.AuthorNames类似“%@author%”
然后
comm.Parameters.AddWithValue(“author”,author)
仍然nothing@user1215114您应该使用
CONCAT
功能。我的答案现在已更新。