473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

SQL EFFICIENCY: using directive vs try/catch

Hi there!

Problem:
I am refactoring all my database code because I noticed every few days
sqlservr.exe would show as Mem Usage of 500,000k in the task manager and
crash our server.

Solution:
I wanted to be able to use the .Dispose() command and found the "using"
directive would do the trick. I have refactored a few try/catch/finally
blocks but found there is no way to CATCH the error and that is where I
usually write to an error log on the server describing the error.

For example here is some code:
using (SqlConnection sqlConn = new
SqlConnection(Constants.DATABASE_CONNECTION_STRING ))
{
using (SqlCommand sqlCmd = new SqlCommand(strSQL, sqlConn))
{
sqlConn.Open();
using (SqlDataReader sqlReader = sqlCmd.ExecuteReader())
{
if (sqlReader.HasRows)
{
while (sqlReader.Read())
{
// Some database code here
}
}
}
}
}

See, there is no try/catch block AT ALL!

Question:
If I code things this way, will I always miss out on catching errors? In
any program, the error could happen anywhere. A "normal" way would just be
to use the try/catch at the sqlReader.Read() part, but what if the error
happens at sqlConn.Open(), then we'd miss the error completely and the
program would crash right?

So, as far as .Dispose() goes this seems the way to go for resource/garbage
handling, but if you can't trap errors why would someone want to use this?
Again, If we received an error on sqlConn.Open() wouldn't the program crash?
There is see no catching the program crash unless we got rid of the "using"
directive right?

Please advise. Thanks in advance.

R Reyes
Feb 18 '08 #1
0 1154

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Junior | last post by:
I keep receiving this "The type or namespace name 'CASsEventHandler' could not be found (are you missing a using directive or an assembly reference?)" message in two particular lines, and I've...
2
by: trying_to_learn | last post by:
im in the primary stages of learning C++. The book im learning from says Dont use using namespace.. directive in header file However im trying to make the following header file.I need to include...
2
by: Reena | last post by:
Hi, Working on .NET 2003. Using C# as server code. I have created a class with following code... using System; using System.Data.OracleClient; using System.Web; using...
3
by: Hubert Hermanutz | last post by:
Hello, for "own directives" I will define a preprocessor constant, as it was possible with MFC with the directive "#define". Is it with C# possible for followed C# snippet: } catch...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
13
by: rincewind | last post by:
I remember reading an article (was it Herb Sutter's?) that recommended avoiding using directives. While I quite understand this recommendation for headers, what's wrong in using directive in .cpp...
3
by: Wayne Shu | last post by:
When I read the chapter of the namespace of the book C++ Primer(3e). It explain the using directive as follow: "A using directive makes the namespace member names visible as if they were declared...
43
by: john | last post by:
Hi, in TC++PL 3 on pages 674-675 it is mentioned: "Maybe your first idea for a two-dimensional vector was something like this: class Matrix { valarray< valarray<doublev; public: // ... };
11
by: subramanian100in | last post by:
Suppose the following is in Test.h #ifndef TEST_H #define TEST_H #include <iostream> #include <string> using namespace std;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.