I have a static DataAccess class with static methods: MyExecuteReader(returns sqldatareader) and MyExecuteXmlReader (returns XmlReader).
Now with ExecuteReader method I can do SqlCommand.ExecuteReader(CommandBehavior.CloseConnection) and this will close the connection when I close the reader.
SqlCommand.ExecuteXmlReader() does not have a similar method.
So what is the best way in this case to ensure the corresponding connection is closed?
Note:
My query returns XML i.e. uses FOR XML AUTO. That is the reason I am using XmlReader. The returned field is considered as SqlString. I can use SqlDataReader.GetSqlString method but I am not sure that is a good approach or not.
Update:
I can read the XML into a string and return string instead of XmlReader from the DAL.
I am aware of the using statement and somehow that option was ruled out I believe due to some exception handling issues.
I ended up modifying the MyExecuteXmlReader (returns XmlReader) in my DAL to MyExecuteXmlReader (returns string).
So now I am reading from XmlReader to a string and return this string instead of XmlReader and that solves the problem of closing the reader and connection outside of DAL.