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(); 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
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();
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?
<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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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'");...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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,...
|
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...
| |