473,406 Members | 2,467 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,406 software developers and data experts.

SQL Encapsulation Functions

I have made some functions to help with some of the more routine things
I do. I just wanted to see if they were "safe" for a production
setting?

public static System.Data.SqlClient.SqlConnection DbConnect()
{
System.Data.SqlClient.SqlConnection tConn = new
SqlConnection(strConn);
tConn.Open();
return tConn;
}

public static void ExecuteNonQuery(string strQuery)
{
SqlConnection tConn = DbConnect();
SqlCommand cmd = new SqlCommand(strQuery,tConn);
cmd.ExecuteNonQuery();
cmd.Dispose();
tConn.Close();
tConn.Dispose();
}

public static string ExecuteScalar(string strQuery)
{
string tString = "";
SqlConnection tConn = DbConnect();
SqlCommand cmd = new SqlCommand(strQuery,tConn);
tString = cmd.ExecuteScalar().ToString();
cmd.Dispose();
tConn.Close();
tConn.Dispose();

return tString;
}

public static SqlDataReader ExecuteReader(string strQuery)
{
SqlConnection drConn = DbConnect();
SqlCommand drCmd = new SqlCommand(strQuery,drConn);
return drCmd.ExecuteReader(CommandBehavior.CloseConnectio n);
}

Please excuse the poor wrapping....

Nov 22 '05 #1
3 1635
<mm******@gmail.com> wrote:
I have made some functions to help with some of the more routine things
I do. I just wanted to see if they were "safe" for a production
setting?


They're not quite - if an exception is thrown, you'll leak the
connection/command. The using statement makes that easy to avoid
though. For example:

public static void ExecuteNonQuery(string strQuery)
{
using (SqlConnection tConn = DbConnect())
{
using (SqlCommand cmd = new SqlCommand(strQuery,tConn))
{
cmd.ExecuteNonQuery();
}
}
}

Personally I'd get rid of the DataReader one - it makes it harder to
close the connection reliably in code, doesn't actually save much, and
shouldn't be used that often anyway.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #2

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<mm******@gmail.com> wrote:
I have made some functions to help with some of the more routine things
I do. I just wanted to see if they were "safe" for a production
setting?


They're not quite - if an exception is thrown, you'll leak the
connection/command. The using statement makes that easy to avoid
though. For example:

public static void ExecuteNonQuery(string strQuery)
{
using (SqlConnection tConn = DbConnect())
{
using (SqlCommand cmd = new SqlCommand(strQuery,tConn))
{
cmd.ExecuteNonQuery();
}
}
}

Personally I'd get rid of the DataReader one - it makes it harder to
close the connection reliably in code, doesn't actually save much, and
shouldn't be used that often anyway.


Ok, what should I use for object binding if DataReaders shouldn't be used?
Also, I have been using DataReaders much like I used ADO recordsets in
classic ASP.

Matthew
Nov 22 '05 #3
Matthew Morvant <ne**@bigfatreddog.com> wrote:
Personally I'd get rid of the DataReader one - it makes it harder to
close the connection reliably in code, doesn't actually save much, and
shouldn't be used that often anyway.
Ok, what should I use for object binding if DataReaders shouldn't be used?


DataTables, usually.
Also, I have been using DataReaders much like I used ADO recordsets in
classic ASP.


I haven't used straight ADO much, so can't really comment on it, but
using a disconnected model is generally preferred these days. There are
some people who think DataReaders shouldn't even be exposed - I
disagree with that, as there are some cases where it's handy to be able
to process everything in an "as you go" fashion, but I think it's
relatively rare.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #4

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

Similar topics

4
by: Marc Schellens | last post by:
If I have a base class with a public virtual member function, a child class will grand access to its overridden memeber function even if its private in the child class, as the compliler cannot...
4
by: Thomas Matthews | last post by:
Hi, I have several translation units (modules) which contain static {local} variables. These function have short global accessor functions. I would like to change these functions into macros,...
3
by: K.K. | last post by:
Consider the following code: >>>>>>>>>>> // Define an empty class public class ZorgleCollection : Dictionary<string, Zorgle> { } // Somewhere outside ZorgleCollection:
3
by: enchantingdb | last post by:
I have an exam tomorrow that covers the perceived advantages and disadvantages of object oriented programming, in particular polymorphism, inheritance and encapsulation. I know the advantages but...
6
by: William H. Burling | last post by:
I am not sure I understand encapsulation. I thought that one of the objectives in OOPs is to hide data structures from other objects so that they did not have to know how to unpack them. ...
5
by: jmsantoss | last post by:
Hi, This is a design question. I have a class named "DataBuffer" that stores some data. After "DataBuffer" is created it can not be modified. All the methods of "DataBuffer" are const as data...
47
by: Roger Lakner | last post by:
I often see operator implemented something like this: class Foo { ... }; class FooList { public: const Foo& operator (unsigned index) const {return array;}; Foo& operator (unsigned index) ...
32
by: bluejack | last post by:
Ahoy: For as long as I've been using C, I've vacillated on the optimal degree of encapsulation in my designs. At a minimum, I aggregate data and code that operate on that data into classlike...
16
by: copx | last post by:
I have recently read "Everything you ever wanted to know about C types" by Peter Seebach (1), and learned about incomplete types. Now, I realise the value of encapsulation, but I wonder whether it...
2
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.