Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
C# 2.0 Return DataReader object
Message
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 2.0
Miscellaneous
Thread ID:
01537785
Message ID:
01537821
Views:
26
>>>>>>I have a method that needs to return either OleDbDataReader or SqlDataReader (depends on the back-end database). Within the method I have IF/ELSE where the code is processed accordingly. But in the method signature I can only specify one type of reader. E.g.
>>>>>>
>>>>>>public OleDbDataReader MethodName()
>>>>>>or
>>>>>>public SqlDataReader MethodName()
>>>>>>
>>>>>>
>>>>>>Does it mean that I have to split the method into two methods depending on the type of reader I need to return and deal with it at the calling level?
>>>>>
>>>>>You need to go generic. Here is an example:
>>>>>
>>>>>
>>>>>        Private oCommand As IDbCommand = Nothing
>>>>>        Private oCommandText As New Collection
>>>>>        Private oDataAdapter As IDbDataAdapter = Nothing
>>>>>        Private oDataParameter As IDbDataParameter = Nothing
>>>>>        Private oDataReader As IDataReader = Nothing
>>>>>
>>>>>
>>>>>Then, you can use the same properties no matter your backend.
>>>>
>>>>Thank you. Does it mean that in the signature of the method I will have
>>>>
>>>>public oDataReader MethodName()
>>>>
>>>
>>>No, it will be
>>>public IDataReader MethodName() // that returns DataReader object
>>>
>>>But do you need to return data reader back? You will not be able to use using then, I think?
>>
>>Yes, my code that calls this method expects a data reader to be returned (which is shown on the page). And if you are saying it will not work, I will have to split my method into two and call the one according to the back end.
>>Thank you.
>
>No, it will work, I think, but you will need to close the reader in your code and also do try/catch in your code. What Michel was showing earlier with USING syntax I don't see how to apply for your scenario if you create reader in one method and work with it in another.

I am sure Michel know this very well. It is just that he knows VB.NET very well and I don't know neither VB.NET no C# <g>. I am looking at the code that John Baird wrote. Thank you.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform