Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

My database class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
craigclister@yahoo.co.uk - 15 Jan 2008 17:24 GMT
I'm writing a small 'learning' app. It uses SQL Server.

I am trying to create a common class of database functions. So, am I
right in saying that it should be a static class? I've done that, but
then I can't declare a private class variable, because the class is
static...

internal static class Database
   {
       internal SqlConnection conn = new SqlConnection(); <---
Error...

       internal static bool Connect()
       {
           try
           {
               conn.ConnectionString = "Data Source=localhost;
Integrated Security=SSPI; Initial Catalog=HomeShopper";
               conn.Open();
               return true;
           }
           catch(Exception e)
           {.................................

How should I be doing this? I'd like to then be able to:

Common.Database.Connect();
Jon Skeet [C# MVP] - 15 Jan 2008 17:46 GMT
> I'm writing a small 'learning' app. It uses SQL Server.
>
[quoted text clipped - 23 lines]
>
> Common.Database.Connect();

Well, to do that you could make conn static (preferrably private, too)
- but maintaining a single static connection isn't a generally good
idea. It's better to open the connection when need it, and close it
immediately after you've used it, e.g. in a using statement.

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

craigclister@yahoo.co.uk - 15 Jan 2008 19:06 GMT
> It's better to open the connection when need it, and close it
> immediately after you've used it, e.g. in a using statement.

Thanks (again) John. Can you give an example of the Using?
I guess you mean if I have a 'getData' type function which I pass an
SQL statement to, and returns a recordset, then the getData would open
the connection, do the query, return the data and close the connection?
Jon Skeet [C# MVP] - 15 Jan 2008 21:10 GMT
> > It's better to open the connection when need it, and close it
> > immediately after you've used it, e.g. in a using statement.
>
> Thanks (again) John. Can you give an example of the Using?

The using statement is something like:

using (SqlConnection conn = new SqlConnection())
{
  // Use conn here
}
// conn has automatically been closed (Dispose has been called) even
// if an exception is thrown

> I guess you mean if I have a 'getData' type function which I pass an
> SQL statement to, and returns a recordset, then the getData would open
> the connection, do the query, return the data and close the connection?

Exactly.

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

Ignacio Machin ( .NET/ C# MVP ) - 15 Jan 2008 18:34 GMT
Hi,

If the class is static then it should contani only static members. Not only
that but that code should not compile and should give you an error that a
static class can only contains static members.

Signature

Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.

> I'm writing a small 'learning' app. It uses SQL Server.
>
[quoted text clipped - 23 lines]
>
> Common.Database.Connect();

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.