C# try-catch and return values

Soldato
Joined
16 Nov 2003
Posts
9,682
Location
On the pale blue dot
Hi guys I have a bit of code in a function that queries a SQL Server database. The query may be bad, the server may not respond etc so I wrap the command up in a try-catch block. Of course this means in theory the SqlDataReader I return out of the function might be uninitialised, though in practice the catch block will kill the program.

How can I modify this code so I can keep the query in the try-catch block but allow the code code to build? SqlDataReader doesn't have a constructor.



Code:
SqlDataReader result;
SqlCommand command = new SqlCommand(queryString, connection);
try
{
    result = command.ExecuteReader();
}
catch (Exception e)
{
    // die
}
return result;
 
Back
Top Bottom