【问题标题】:Inserting multiple records into MySQL using MySqlCommand in one query C# (MySQLConnector) & command Parameters for escaping quotes在一个查询中使用 MySqlCommand 将多条记录插入 MySQL C# (MySQLConnector) & command 转义引号的参数
【发布时间】:2011-08-28 03:25:51
【问题描述】:

我正在尝试使用 C# 中的 MySqlCommand 对象(使用 MySQL 连接器库)在一个查询中将多条记录插入到一​​个表中。

我知道如何做到这一点的唯一方法是自己动态构建查询并设置command.CommandType = CommandType.Text;

这种方法的问题是字段没有为引号等进行转义。我想我自己可以编写一个函数来逃避这些值,但我在互联网上阅读的每篇文章或问题似乎都不赞成这一点,并说使用command.Parameters 这样更有效、更彻底。

我的问题是我不知道如何为多行设置参数。我该怎么做?

编辑:这是针对 24/7 运行的商业服务,所以我需要找到最有效的方法来做到这一点。我没有使用存储过程 - 这是唯一的方法还是还有其他方法?

    public static string MySqlEscape(object value)
    {
        string val = value.ToString();
        if (val.Contains("'"))
            return val.Replace("'", "' + NCHAR(96) + '");
        else
            return val;
    }

    public void InsertProcessedData(long unprocessedID, long pagerID, long firmwareRelativeProtocolID, DataTable processedData)
    {
        using(processedData)
        {
            string paramColNames = string.Empty;
            for(int i =1;i<=processedData.Columns.Count;i+=1)
            {
                paramColNames+=string.Format("Param{0}",i);
                if(i!=processedData.Columns.Count)
                    paramColNames+=",";
            }

            string SQL = "INSERT INTO gprs_data_processed (@UnprocessedID,@PagerID,@FirmwareRelativeProtocolID,"+paramColNames+") VALUES ";

            for (int i = 0; i < processedData.Rows.Count;i+=1)
            {
                SQL += string.Format("({0},{1},{2},", unprocessedID, pagerID, firmwareRelativeProtocolID);
                for (int c = 0; c < processedData.Columns.Count; c += 1)
                {
                    SQL += string.Format("'{0}'", MySqlEscape(processedData.Rows[i][c]));
                    if (i != processedData.Columns.Count)
                        SQL += ",";
                }
                SQL+=")";
                if (i + 1 != processedData.Rows.Count)
                    SQL += ",";
                else
                    SQL += ";";
            }

            using (MySqlConnection connection = new MySqlConnection(_connection))
            {
                connection.Open();
                using (MySqlCommand command = connection.CreateCommand())
                {
                    command.CommandType = CommandType.Text;
                    command.CommandText = SQL;
                    command.ExecuteNonQuery();
                }
                connection.Close();
            }
        }
    }

【问题讨论】:

    标签: c# .net mysql sql mysql-connector


    【解决方案1】:

    我不确定如何创建单个命令。我所做的是创建一个使用参数的方法,然后传入我想要一次运行的值。

    我的方法:

    public void Insert(string strSQL, string[,] parameterValue)
            {
    
                //open connection
                if (this.OpenConnection() == true)
                {
                    //create command and assign the query and connection from the constructor
                    MySqlCommand cmd = new MySqlCommand(strSQL, connection);
    
                    //add parameters
                    for (int i = 0; i < (parameterValue.Length / 2); i++)
                    {
                        cmd.Parameters.AddWithValue(parameterValue[i, 0], parameterValue[i, 1]);
                    }
    
                    //Execute command
                    cmd.ExecuteNonQuery();
    
                    //close connection
                    this.CloseConnection();
                }
            }
    

    【讨论】:

      【解决方案2】:

      我最终只使用了我已经编写的函数,因为它在一个命令中完成了所有操作,并使用:

      public static string MySqlEscape(object value)
      {
          string val = value.ToString();
          if (val.Contains("'"))
              return val.Replace("'", "''");
          else
              return val;
      }
      

      它工作正常,所以我会看看情况如何。

      【讨论】:

      • 我也遇到了同样的情况。问题是,MySQL DotNet 连接器确实有一个转义方法。出于未知原因,我无法使用它。它在 MySql.Data.MySqlClient.MySqlHelper
      猜你喜欢
      • 2016-11-30
      • 2012-12-16
      • 1970-01-01
      • 2015-05-30
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多