Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在一个查询中使用MySqlCommand将多条记录插入MySQL C#(MySQLConnector)&;转义引号的命令参数_C#_.net_Mysql_Sql_Mysql Connector - Fatal编程技术网

在一个查询中使用MySqlCommand将多条记录插入MySQL C#(MySQLConnector)&;转义引号的命令参数

在一个查询中使用MySqlCommand将多条记录插入MySQL C#(MySQLConnector)&;转义引号的命令参数,c#,.net,mysql,sql,mysql-connector,C#,.net,Mysql,Sql,Mysql Connector,我试图使用C#(使用MySQL连接器库)中的MySqlCommand对象在一个查询中将多条记录插入表中 我知道如何做到这一点的唯一方法是自己动态构造查询,并设置command.CommandType=CommandType.Text 此方法的问题是,字段不会对引号等进行转义。我可以自己编写一个函数来逃避我猜想的值,但我在互联网上读到的每一篇文章或问题似乎都对此表示不满,并说使用command.Parameters,因为这样更有效、更彻底 我的问题是我不知道如何为多行设置参数。我该怎么做 编辑:这

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

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

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

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

编辑:这是一项全天候运行的商业服务,因此我需要找到最有效的方法。我没有使用存储过程-这是唯一的方法还是有其他方法

    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();
            }
        }
    }
公共静态字符串MySqlEscape(对象值)
{
字符串val=value.ToString();
if(val.Contains(“'))
返回值替换(“'”,“'+NCHAR(96)+'”;
其他的
返回val;
}
public void InsertProcessedData(长未处理ID、长分页ID、长firmwareRelativeProtocolID、数据表processedData)
{
使用(处理数据)
{
string paramColNames=string.Empty;

对于(int i=1;i我不确定如何创建单个命令。我要做的是创建一个使用参数的方法,然后传入我希望一次运行一个的值

我的方法:

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();
            }
        }
public void Insert(字符串strSQL,字符串[,]参数值)
{
//开放连接
if(this.OpenConnection()==true)
{
//创建命令并从构造函数中分配查询和连接
MySqlCommand cmd=新的MySqlCommand(strSQL,连接);
//添加参数
对于(int i=0;i<(parameterValue.Length/2);i++)
{
cmd.Parameters.AddWithValue(parameterValue[i,0],parameterValue[i,1]);
}
//执行命令
cmd.ExecuteNonQuery();
//密切联系
这个.CloseConnection();
}
}

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

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中