"Mike" <bl***@blank.blankschrieb im Newsbeitrag
news:ud**************@TK2MSFTNGP06.phx.gbl...
All the code samples I can find show using on its own or try... catch..
.finally on its own. Will the following cover me properly in terms of
handling errors and cleaning up?
string conns = "Provider=Microsoft.Jet.OleDb.4.0;Data
Source=|DataDirectory|Northwind.mdb";
try
{
using (OleDbConnection conn = new OleDbConnection(conns))
{
OleDbCommand cmd = new OleDbCommand("SELECT * From FOO", conn);
conn.Open();
OleDbDataReader rd = cmd.ExecuteReader();
//do stuff
}
The using statement ensures, that the resource (in this case, the
OleDbConnection) is disposed on exiting the block of the using statement.
This will work indepently of how the block is exited, wether by normal flow,
an exception or a jumpstatement or what else.
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
This block will handle any excption wich falls out of the try block. Because
it is a general catch block, no exception whatsoever occuring inside the try
block will exit the try-catch-statement.
It this is a good approach can be doubted ;-)
The error handling will occur directly after the disposing of the connection
(if it occured inside the using block).
>
Clearly Access and Response.Write are just being used for demo purposes...
I hope so ;-)
Does this make it clearer to you?
Christof