3

MySqlCommandC#のオブジェクト (MySQL コネクタ ライブラリを使用) を使用して、1 つのクエリで複数のレコードをテーブルに挿入しようとしています。

これを行う方法を知っている唯一の方法は、自分でクエリを動的に構築して設定することですcommand.CommandType = CommandType.Text;

この方法の問題は、フィールドが引用符などでエスケープされないことです。自分で推測する値をエスケープする関数を書くこともできますが、インターネットで読んだすべての記事や質問はこれに眉をひそめているようでcommand.Parameters、これをより効率的かつ徹底的に使用すると述べています。

私の問題は、複数の行のパラメーターを設定する方法がわからないことです。どうやってやるの?

編集:これは24時間年中無休で実行される商用サービス用であるため、これを行う最も効率的な方法を見つける必要があります. 私はストアド プロシージャを使用していません。これが唯一の方法ですか、それとも別の方法がありますか?

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

2 に答える 2

1

単一のコマンドを作成する方法がわかりません。私がしていることは、パラメーターを使用するメソッドを作成し、一度に 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();
            }
        }
于 2011-05-25T14:58:52.747 に答える
1

1つのコマンドですべてを行うため、すでに作成した関数を使用するだけになりました。次を使用します。

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

順調に進んでいるので様子を見てみます。

于 2011-05-27T11:13:31.590 に答える