简体   繁体   中英

Adding parameters to sqlCommand query

I'm doing my first database migration and I'm trying to recreate this sqlCommand so that it will add the correct values to the representative objects.

    string sqlCommand = "UPDATE WITestData SET " +
            "Width01 = ?, Angle01 = ?, Comment01 = ?, " +
            "Width02 = ?, Angle02 = ?, Comment02 = ?, " +
            "Width03 = ?, Angle03 = ?, Comment03 = ?, " +
            "Width04 = ?, Angle04 = ?, Comment04 = ?, " +
            "Width05 = ?, Angle05 = ?, Comment05 = ?, " +
            "Width06 = ?, Angle06 = ?, Comment06 = ?, " +
            "Width07 = ?, Angle07 = ?, Comment07 = ?, " +
            "Width08 = ?, Angle08 = ?, Comment08 = ?, " +
            "Width09 = ?, Angle09 = ?, Comment09 = ?, " +
            "Width10 = ?, Angle10 = ?, Comment10 = ? " +
            "WHERE ReportNumber = @reportNumber";
        var parameters = new object[31];
        for (int i = 0; i < WITestData.WIData.Length; i++)
        {
            parameters[3 * i] = WITestData.WIData[i].Width;
            parameters[3 * i + 1] = WITestData.WIData[i].Angle;
            parameters[3 * i + 2] = WITestData.WIData[i].Comment;

        }  parameters[30] = WITestData.ReportNumber;
        if (ExecuteNonQuery(sqlCommand, parameterList))
        {
            var index = m_DataManager.Database.WITestData.FindIndex(t => t.ReportNumber == WITestData.ReportNumber);
            m_DataManager.Database.WITestData[index] = WITestData;
            return true;
        }
        return false;
    }

I have added the following to the for(...) to assign each object a seperate parameter so it will output the correct values.

 for (int i = 0; i < WITestData.WIData.Length; i++)
        {
            //input Parameters into sqlCommand
            string widthParameter = $"@width{counter}";
            string angleParameter = $"@angle{counter}";
            string commentParameter = $"@comment{counter}";
            using (SqlCommand command = new SqlCommand())
            {
                parameterList.Add(new SqlParameter(widthParameter, WITestData.WIData[i].Width));
                parameterList.Add(new SqlParameter("@angle", angleParameter));
                parameterList.Add(new SqlParameter("@comment", commentParameter));

                parameterList.Add(command.Parameters.AddWithValue("@reportNumber", WITestData.ReportNumber));

                command.Parameters.Clear();
            }
            parameters[3 * i] = WITestData.WIData[i].Width;
            parameters[3 * i + 1] = WITestData.WIData[i].Angle;
            parameters[3 * i + 2] = WITestData.WIData[i].Comment;

            counter++;
        }

Note: I've created an int counter = 1 and a var parameterList = new List<SqlParameter>(); at the top of the method.

How do i input the width/angle/comment parameters into the sqlCommand and make sure that they are correctly assigned with their values?

First thing wrong is the command.Parameters.Clear(); in your code. This removes all parameters from the command, however, it seems that after that line you never readd your parameters to the command object but instead you try to add them to your type undefined object array.

Trying to fix you code I start from the basic information present in your code. Your query requires 31 parameters. So, I assume that WIData.Length is 10 and a final parameter (the ReportNumber ) is added after the loop

You can simplify a lot your code with this approach

// A command requires an sql text and an OPEN connection to execute....
OleDbCommand cmd = new OleDbCommand(sqlCommand, connection);

// Use directly the parameters collection inside the command object
var parameters = cmd.Parameters;

// Loop over the WIData array length (10 times = 30 parameters)
for (int i = 0; i < WITestData.WIData.Length; i++)
{
    // No need to give an unique name, however, if you really want a name 
    //  you can replace ? with 
    // "W"+i.ToString(), "A"+i.ToString() and "C"+i.ToString()
    parameters.Add("?", OleDbType.Decimal).Value = WITestData.WIData[i].Width;
    parameters.Add("?", OleDbType.Decimal).Value = WITestData.WIData[i].Angle;
    parameters.Add("?", OleDbType.VarWChar).Value = WITestData.WIData[i].Comment;
}  
parameters.Add("?", OleDbType.Integer).Value = WITestData.ReportNumber;

I assume also the datatype of the parameters is Decimal, Strings and integer, you should replace the OleDbType if they are not correct, but don't remove the type. It ensure the correct parsing of your values.

This is the standard approach, but if you want to keep your current code then you could change your loop to

for (int i = 0; i < WITestData.WIData.Length; i++)
{
    string widthParameter = $"@width{counter}";
    string angleParameter = $"@angle{counter}";
    string commentParameter = $"@comment{counter}";
    parameterList.Add(widthParameter, OleDbType.Deciaml).Value = WITestData.WIData[i].Width;
    parameterList.Add(angleParameter, OleDbType.Decimal).Value = WITestData.WIData[i].Width;
    parameterList.Add(commentParameter, OleDbType.VarWChar).Value = WITestData.WIData[i].Comment;
    counter++;
}
parameterList.Add("@reportNumber", OleDbType.Integer).Value = WITestData.ReportNumber;

This solved my question.

   int counter = 1;
        var parameterList = new List<SqlParameter>();
        SqlCommand command = new SqlCommand();
        string sqlCommand = $"UPDATE WITestData SET " +
            "Width01 = {widthParameter}, Angle01 = {angleParameter}, Comment01 = {commentParameter}, " +
            "Width02 = {widthParameter}, Angle02 = {angleParameter}, Comment02 = {commentParameter}, " +
            "Width03 = {widthParameter}, Angle03 = {angleParameter}, Comment03 = {commentParameter}, " +
            "Width04 = {widthParameter}, Angle04 = {angleParameter}, Comment04 = {commentParameter}, " +
            "Width05 = {widthParameter}, Angle05 = {angleParameter}, Comment05 = {commentParameter}, " +
            "Width06 = {widthParameter}, Angle06 = {angleParameter}, Comment06 = {commentParameter}, " +
            "Width07 = {widthParameter}, Angle07 = {angleParameter}, Comment07 = {commentParameter}, " +
            "Width08 = {widthParameter}, Angle08 = {angleParameter}, Comment08 = {commentParameter}, " +
            "Width09 = {widthParameter}, Angle09 = {angleParameter}, Comment09 = {commentParameter}, " +
            "Width10 = {widthParameter}, Angle10 = {angleParameter}, Comment10 = {commentParameter} " +
            "WHERE ReportNumber = @reportNumber";
        var parameters = new object[31];
        for (int i = 0; i < WITestData.WIData.Length; i++)
        {
            //input Parameters into sqlCommand
            string widthParameter = $"@width{(counter < 10 ? counter.ToString("D2") : counter.ToString() )}";
            string angleParameter = $"@angle{(counter < 10 ? counter.ToString("D2") : counter.ToString())}";
            string commentParameter = $"@comment{(counter < 10 ? counter.ToString("D2") : counter.ToString())}";
            using (command)
            {
                parameterList.Add(new SqlParameter(widthParameter, WITestData.WIData[i].Width));
                parameterList.Add(new SqlParameter(angleParameter, WITestData.WIData[i].Angle));
                parameterList.Add(new SqlParameter(commentParameter, WITestData.WIData[i].Comment));

                command.Parameters.Clear();
            }
            parameters[3 * i] = WITestData.WIData[i].Width;
            parameters[3 * i + 1] = WITestData.WIData[i].Angle;
            parameters[3 * i + 2] = WITestData.WIData[i].Comment;

            counter++;
        }

The code could probably look better but in that case I would attend to that at a later clean-up stage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM