如何将command.Parameters.AddWithValue与值一起使用?

时间:2019-02-21 12:27:02

标签: c# mysql

我的代码如下:

if (!string.IsNullOrEmpty(Mpdue.TheObjectPropertyNameNs))
{

    string sql = @String.Format(" SELECT * FROM doTable WHERE dOCode IN ('" + Mpdue.TheObjectPropertyNameNs + "'); ");

    using (OdbcConnection myConnectionString =
        new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
    {
        using (OdbcCommand command =
            new OdbcCommand(sql, myConnectionString))
        {
            try
            {
                command.Connection.Open();

                using (OdbcDataReader reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            Response.Write(reader["dOCode"].ToString() + "<br />");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("operation failed!", ex);
            }
            finally
            {
                command.Connection.Close();
            }
        }
    }
 }

输出:

D410
D420
D430
D440

现在,我尝试在command.Parameters.AddWithValue查询中使用MySql

我一直在尝试并且找不到错误,使用command.Parameters.AddWithValue时的输出为空,为什么?

if (!string.IsNullOrEmpty(Mpdue.TheObjectPropertyNameNs))
{

    string sql = @String.Format(" SELECT * FROM doTable WHERE dOCode IN (?); ");

    using (OdbcConnection myConnectionString =
        new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
    {
        using (OdbcCommand command =
            new OdbcCommand(sql, myConnectionString))
        {
            try
            {
                command.Connection.Open();
                command.Parameters.AddWithValue("param1", Mpdue.TheObjectPropertyNameNs);

                using (OdbcDataReader reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            Response.Write(reader["dOCode"].ToString() + "<br />");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("operation failed!", ex);
            }
            finally
            {
                command.Connection.Close();
            }
        }
    }
}

编辑#2

var parameters = new string[Mpdue.TheObjectPropertyNameNs.Length];
var paramValue = string.Join(", ", parameters);


string sql = string.Format("SELECT * FROM doTable WHERE dOCode IN ({0})", string.Join(", ", parameters.Select(x => "?")));

int index = 0;
foreach (var param in parameters)
{
   command.Parameters.AddWithValue("param{index}", param);
   index++;
}

编辑#1

我的新代码如下:

if (!string.IsNullOrEmpty(Mpdue.TheObjectPropertyNameNs))
{
   var parameters = new string[Mpdue.TheObjectPropertyNameNs.Length]; 

   string sql = @String.Format(" SELECT * FROM doTable WHERE dOCode IN ({0})", string.Join(", ", parameters));

        using (OdbcConnection myConnectionString =
            new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
        {
            using (OdbcCommand command =
                new OdbcCommand(sql, myConnectionString))
            {
                try
                {
                    command.Connection.Open();

                    for (int i = 0; i < Mpdue.TheObjectPropertyNameNs.TrimStart('\'').TrimEnd('\'').Length; i++)
                    {
                       parameters[i] = string.Format("param1{0}", i);
                       command.Parameters.AddWithValue(parameters[i], Mpdue.TheObjectPropertyNameNs.TrimStart('\'').TrimEnd('\'')[i]);
                    }                    

                    using (OdbcDataReader reader = command.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                Response.Write(reader["dOCode"].ToString() + "<br />");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new ApplicationException("operation failed!", ex);
                }
                finally
                {
                    command.Connection.Close();
                }
            }
        }
     }

错误:

  

错误[42000] [MySQL] [ODBC 5.1驱动程序] [mysqld-5.5.24-log]   您的SQL语法错误;检查与您的手册相对应的手册   MySQL服务器版本,可在',,,,,,,附近使用正确的语法   ,,,,,,,,,,,,,,,,,,,,,,,)在第1行

2 个答案:

答案 0 :(得分:1)

为了解决这个问题,您可以将查询更改为以下字符串:

SELECT * FROM doTable WHERE FIND_IN_SET(dOCode, ?)

然后,您需要确保以D410, D420, D430, D440的形式设置参数。因此,您可以像

这样的第一种方法来构建它
var paramValue = string.Join(", ", parameters);

并稍后添加

command.Parameters.AddWithValue("param1", paramValue);

请注意,使用FIND_IN_SET可能会对性能产生负面影响,因为它将扫描整个表,而不管索引如何。一种替代方法是在查询中插入n个参数,然后分别添加每个参数,例如

string sql = string.Format("SELECT * FROM doTable WHERE dOCode IN ({0})", string.Join(", ", parameters.Select(x => "?")));
// ...
int index = 0;
foreach(var param in parameters)
{
  command.Parameters.AddWithValue($"param{index}", param);
  index++;
}

如果期望的参数数量不太高,这是一种可行的方法。

答案 1 :(得分:1)

在C#中尝试此操作。希望我能帮上忙。

    if (!string.IsNullOrEmpty(Mpdue.TheObjectPropertyNameNs))
    {
        var paramValue = string.Join(", ", Mpdue.TheObjectPropertyNameNs);

        string sql = string.Format("SELECT * FROM doTable WHERE dOCode IN ({0})", paramValue.ToString());

        ...

        foreach (var param in paramValue)
        {
           command.Parameters.AddWithValue("param1", param.ToString());
        }

        ...
   }