Skip to content

Setting ParameterName after adding to DbCommand #811

Description

@tznind

I think I have hit an issue changing from MySql.Data to MySqlConnector that appears to be down to me changing a DbParameter name after adding it to the command (SMI/RdmpDicom@3436074).

I have written the following unit test that seems to repro the issue.

[TestCase(true)]
[TestCase(false)]
public void TestNamingParameterBeforeAdding(bool nameBeforeAdd)
{
    var cmd = new MySqlCommand();
    var p = cmd.CreateParameter();

    if(nameBeforeAdd)
        p.ParameterName = "@ff";

    cmd.Parameters.Add(p);

    if(!nameBeforeAdd) 
        p.ParameterName = "@ff";
        
    p.Value = "bb";

    Assert.AreEqual("bb",cmd.Parameters["@ff"].Value);
}

Results are:

Rdmp.Dicom.Tests.Unit.Test.TestNamingParameterBeforeAdding(True):
    Outcome: Passed
    
Rdmp.Dicom.Tests.Unit.Test.TestNamingParameterBeforeAdding(False):
    Outcome: Failed
    Error Message:
    System.ArgumentException : Parameter '@ff' not found in the collection
Parameter name: parameterName
    Stack Trace:
       at MySql.Data.MySqlClient.MySqlParameterCollection.GetParameter(String parameterName) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlParameterCollection.cs:line 86
   at MySql.Data.MySqlClient.MySqlParameterCollection.get_Item(String name) in C:\projects\mysqlconnector\src\MySqlConnector\MySql.Data.MySqlClient\MySqlParameterCollection.cs:line 152
   at Rdmp.Dicom.Tests.Unit.Test.TestNamingParameterBeforeAdding(Boolean nameBeforeAdd) in /home/thomas/RdmpDicom/Rdmp.Dicom.Tests/Unit/AmbiguousFilePathTests.cs:line 33
    
Total tests: 2. Passed: 1. Failed: 1. Skipped: 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions