>> <snip>
>>
[quoted text clipped - 40 lines]
> Windows Service). I don't see where in you setup I can do that without
> the try/catch.
In that case, you would need try/catch
> Also, in my actual code I also want to use the same connection to run
> multiple commands. So using your example, how would I deal with the
[quoted text clipped - 12 lines]
> cmd = new SqlCommand("proc" + ptn.TableName + "_insert",
> conn);
change that to:
using (SqlCommand cmd = new SqlCommand("proc" + ptn.TableName +
"_insert", conn);
> cmd.CommandType = CommandType.StoredProcedure;
> ...
[quoted text clipped - 15 lines]
> You said the Dspose would abort the tansaction? Even if I don't actually
> call Dispose()?
"using" implicitly calls dispose when the object defined in the using
statement goes out of scope.
So, if I do something like this:
using (MyObject myOb = new MyObject())
{
... do something here ...
}
myOb.Dispose()
The myOb.Dispose() call would fail. First of all, myOb is out of scope.
Second of all, myOb was disposed when the closing brace "}" was executed.
> By abort the transaction I assume that you mean rollback?
Correct.
> Why would that be? Would it be do to closing the connection with a commit
> that Sql Server would decide to roll it back?
It's simply the way SqlTransaction.Dispose() works. If the transaction is
disposed without your code excplicitly calling Commit(), it will rollback,
on the assumption that something failed. This makes perfect sense in a using
block since, if you get an unhandled exception during execution, the
exception will take you outside of the using block, thus calling Dispose()
on the transaction and properly rolling it back instead of committing it.
> I assume that the Using statement would close the statement at the end of
> the Using clause.
>
> Not the end of the "using" clause?
Not sure what you mean here. It's kind of like this:
using (MyObject myOb = new MyObject())
{
... do something here ...
} <--- Dispose() called here, more or less
> Thanks,
>
> Tom
Fredo - 26 Feb 2008 00:31 GMT
In the part where I wrote:
using (SqlCommand cmd = new SqlCommand("proc" + ptn.TableName +
"_insert", conn);
what I really meant was:
using (SqlCommand cmd = new SqlCommand("proc" + ptn.TableName +
"_insert", conn)
{
cmd.CommandType = CommandType.StoredProcedure;
...
dbReader = cmd.ExecuteReader();
}
Sorry, just wanted to clarify.
tshad - 26 Feb 2008 00:54 GMT
> In the part where I wrote:
>
[quoted text clipped - 12 lines]
>
> Sorry, just wanted to clarify.
So then I would have:
string errorMessage = "";
using (SqlConnection conn = new SqlConnection(connectionString))
{
try
{
foreach(...)
{
using (SqlCommand cmd = new SqlCommand("proc" + ptn.TableName +
"_insert", conn)
{
cmd.CommandType = CommandType.StoredProcedure;
...
dbReader = cmd.ExecuteReader();
...
}
}
}
catch(Exception exc)
{
errorMessage = exc.Message;
}
}
return errorMessage;
So at then end of the "using" block of code for the SqlCommand - dispose is
called and cmd is closed and in the using block for SqlConnection - the
SqlConnection is closed at the end.
Thanks,
Tom
<snip>
> > Note that your original code would have thrown a NullReferenceException
> > had you been unable to open the database connection or begin the
[quoted text clipped - 4 lines]
> Windows Service). I don't see where in you setup I can do that without the
> try/catch.
<snip>
Fredo has responded to the other points.
I suspect it's unlikely that this code appears in the top level of your
service. You're likely to need to catch and log errors which occur
elsewhere, right?
Just catch them near the top of your call stack, and log them there,
rather than logging them in the data access layer. The same information
will still be available, and it means having far fewer catch blocks
which do very little.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
tshad - 26 Feb 2008 00:47 GMT
> <snip>
>
[quoted text clipped - 20 lines]
> will still be available, and it means having far fewer catch blocks
> which do very little.
Makes sense.
Thanks,
Tom