diff --git a/src/System.Data.SqlClient/src/System/Data/SqlClient/SqlParameter.cs b/src/System.Data.SqlClient/src/System/Data/SqlClient/SqlParameter.cs index d2d8fa52d68f..71071115b902 100644 --- a/src/System.Data.SqlClient/src/System/Data/SqlClient/SqlParameter.cs +++ b/src/System.Data.SqlClient/src/System/Data/SqlClient/SqlParameter.cs @@ -261,15 +261,8 @@ public string XmlSchemaCollectionDatabase } set { - bool collectionIsNull = _xmlSchemaCollection != null; - if (collectionIsNull) - { - _xmlSchemaCollection = new SqlMetaDataXmlSchemaCollection(); - } - if (value != null || collectionIsNull) - { - _xmlSchemaCollection.Database = value; - } + EnsureXmlSchemaCollectionExists(); + _xmlSchemaCollection.Database = value; } } @@ -281,15 +274,8 @@ public string XmlSchemaCollectionOwningSchema } set { - bool collectionIsNull = _xmlSchemaCollection != null; - if (collectionIsNull) - { - _xmlSchemaCollection = new SqlMetaDataXmlSchemaCollection(); - } - if (value != null || collectionIsNull) - { - _xmlSchemaCollection.OwningSchema = value; - } + EnsureXmlSchemaCollectionExists(); + _xmlSchemaCollection.OwningSchema = value; } } @@ -301,15 +287,8 @@ public string XmlSchemaCollectionName } set { - bool collectionIsNull = _xmlSchemaCollection != null; - if (collectionIsNull) - { - _xmlSchemaCollection = new SqlMetaDataXmlSchemaCollection(); - } - if (value != null || collectionIsNull) - { - _xmlSchemaCollection.Name = value; - } + EnsureXmlSchemaCollectionExists(); + _xmlSchemaCollection.Name = value; } } @@ -1977,6 +1956,14 @@ internal static string[] ParseTypeName(string typeName, bool isUdtTypeName) } } } + + private void EnsureXmlSchemaCollectionExists() + { + if (_xmlSchemaCollection is null) + { + _xmlSchemaCollection = new SqlMetaDataXmlSchemaCollection(); + } + } internal sealed class SqlParameterConverter : ExpandableObjectConverter { diff --git a/src/System.Data.SqlClient/tests/FunctionalTests/SqlParameterTest.cs b/src/System.Data.SqlClient/tests/FunctionalTests/SqlParameterTest.cs index bd8885164e4e..40762d0f4559 100644 --- a/src/System.Data.SqlClient/tests/FunctionalTests/SqlParameterTest.cs +++ b/src/System.Data.SqlClient/tests/FunctionalTests/SqlParameterTest.cs @@ -38,5 +38,69 @@ public void ParameterPrecisionOnBaseType() Assert.Equal(10, parameter.Precision); Assert.Equal(5, parameter.Scale); } + + [Fact] + public void CreateParameterWithValidXmlSchema() + { + string xmlDatabase = "database"; + string xmlSchema = "schema"; + string xmlName = "name"; + + SqlParameter parameter = new SqlParameter("@name", SqlDbType.Int, 4, ParameterDirection.Input, 0, 0, "name", DataRowVersion.Original, false, 1, xmlDatabase, xmlSchema, xmlName); + + Assert.Equal(xmlDatabase, parameter.XmlSchemaCollectionDatabase); + Assert.Equal(xmlSchema, parameter.XmlSchemaCollectionOwningSchema); + Assert.Equal(xmlName, parameter.XmlSchemaCollectionName); + } + + [Fact] + public void CreateParameterWithEmptyXmlSchema() + { + SqlParameter parameter = new SqlParameter("@name", SqlDbType.Int, 4, ParameterDirection.Input, 0, 0, "name", DataRowVersion.Original, false, 1, string.Empty, string.Empty, string.Empty); + + Assert.Equal(string.Empty, parameter.XmlSchemaCollectionDatabase); + Assert.Equal(string.Empty, parameter.XmlSchemaCollectionOwningSchema); + Assert.Equal(string.Empty, parameter.XmlSchemaCollectionName); + } + + [Fact] + public void CreateParameterWithNullXmlSchema() + { + SqlParameter parameter = new SqlParameter("@name", SqlDbType.Int, 4, ParameterDirection.Input, 0, 0, "name", DataRowVersion.Original, false, 1, null, null, null); + + Assert.Equal(string.Empty, parameter.XmlSchemaCollectionDatabase); + Assert.Equal(string.Empty, parameter.XmlSchemaCollectionOwningSchema); + Assert.Equal(string.Empty, parameter.XmlSchemaCollectionName); + } + + [Fact] + public void CreateParameterWithoutXmlSchema() + { + SqlParameter parameter = new SqlParameter(); + + Assert.Equal(string.Empty, parameter.XmlSchemaCollectionDatabase); + Assert.Equal(string.Empty, parameter.XmlSchemaCollectionOwningSchema); + Assert.Equal(string.Empty, parameter.XmlSchemaCollectionName); + } + + [Fact] + public void SetParameterXmlSchema() + { + SqlParameter parameter = new SqlParameter(); + + Assert.Equal(string.Empty, parameter.XmlSchemaCollectionName); + + // verify that if we set it to null we still get an empty string back + parameter.XmlSchemaCollectionName = null; + Assert.Equal(string.Empty, parameter.XmlSchemaCollectionName); + + // verify that if we set a value we get it back + parameter.XmlSchemaCollectionName = "name"; + Assert.Equal("name", parameter.XmlSchemaCollectionName); + + // verify that if we set it explicitly to null it reverts to empty string + parameter.XmlSchemaCollectionName = null; + Assert.Equal(string.Empty, parameter.XmlSchemaCollectionName); + } } }