473,395 Members | 1,458 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,395 software developers and data experts.

My database class

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();
Jan 15 '08 #1
4 4057
<cr**********@yahoo.co.ukwrote:
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();
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.

--
Jon Skeet - <sk***@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
Jan 15 '08 #2
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.

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
<cr**********@yahoo.co.ukwrote in message
news:27**********************************@m34g2000 hsf.googlegroups.com...
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();

Jan 15 '08 #3
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?
Jan 15 '08 #4
<cr**********@yahoo.co.ukwrote:
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.

--
Jon Skeet - <sk***@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
Jan 15 '08 #5

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

Similar topics

2
by: lawrence | last post by:
I had some code that worked fine for several weeks, and then yesterday it stopped working. I'm not sure what I did. Nor can I make out why it isn't working. I'm running a query that should return 3...
5
by: lawrence | last post by:
I posted before, but have now narrowed my problem down to this method. At the start of the method, I test to make sure that I have a resource, a pointer to data returned from a database. This test...
1
by: Johan | last post by:
Hi, The problem is : You have a class A with one or more data variables. You want to store the data variables in a database for example MySQL. Also you do not want to have SQL code in your...
2
by: Bryan | last post by:
Hello, I'm just starting to develop in asp.net and i have a question about using a database connection globally in my app. I have set up the procedures for getting all my connection string info...
14
by: Nick Gilbert | last post by:
Hi, I have an asp.net application which runs from a CD-ROM using Cassini. As such, it is single user only. The application connects to an Access database when it is loaded, and keeps the same...
35
by: Terry Jolly | last post by:
Web Solution Goal: Have a global database connection Why: (There will be 30+ tables, represented by 30+ classes) I only want to reference the database connection once. I put the connection...
3
by: josh.kuo | last post by:
Sorry about the subject, I can't think of a better one. I recently wrote some PHP classes that I think might be of interest to this group. Since I have been reaping the benefits of reading news...
5
by: Slant | last post by:
Here's a question that most will have different answers to. I'm just dying to find a solution that seems halfway automated!! There really are two seperate issues which might be answered by the...
1
tolkienarda
by: tolkienarda | last post by:
i need to update a database table using variables in unusual places here are the update statements mysql_query("UPDATE 'grades' SET '$class' = '$grade' WHERE student='$student'");...
22
by: amygdala | last post by:
Hi, I'm trying to grasp OOP to build an interface using class objects that lets me access database tables easily. You have probably seen this before, or maybe even built it yourself at some...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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: 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
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
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
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
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...

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.