473,795 Members | 3,151 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Having a connection class that only does connection work

Jim
Is it possible to have a Connection class that opens up connection to my DB
and leaves it open until I decide to close it?
I run many SQL statements so I was wondering if it's possible to call a
Class1.GetProdu cts that sets up an SQL statement and then calls another
Class2.Connect that actually opens up the connection Class2.Connect would
not receive anything (since the connections string is always the same) and
would not return anything.

Thanks.
Nov 15 '05 #1
2 2128
> Is it possible to have a Connection class that opens up connection to my
DB
and leaves it open until I decide to close it?
In many instances, if you are using any of the 'connected' ado.net objects,
you don't have a choice, you have to open them manually and close them when
you are done which is determined by you.
I run many SQL statements so I was wondering if it's possible to call a
Class1.GetProdu cts that sets up an SQL statement and then calls another
Class2.Connect that actually opens up the connection Class2.Connect would
not receive anything (since the connections string is always the same) and
would not return anything.
Yes, you can do that, but if I understand you correctly, I don't understand
what you are getting by it. You can define a static property connection
somewhere and instantiate it in your class locally each time you need it.
Leaving a connection open just for leaving it's open sake is kind of
antithetical to everything ADO.NET, and unless you are firing A LOT of
sequential statements, I don't know that there's much value if the
connection string remains identical and everything is sitting tin the pool.

If you could describe your ultimate goal, I could probably be of more
assistance, but for the time being, I think theanswer is Yes to both of your
questions.

Cheers,

Bill
"Jim" <NA> wrote in message news:uI******** ******@TK2MSFTN GP11.phx.gbl...

Thanks.

Nov 15 '05 #2
Jim
I want my code to be as easy to maintain as possible. Basically, to get any
records from my DB, I use two classes: one that sets up the SQL statement,
and another that makes the connection (plus the class that contains the
windows form). Now, is this the best way to divide my tasks? And how can I
update the dataset in frm_AddNewProdu ct?

This is how it looks like:
public class frm_AddNewProdu ct : System.Windows. Forms.Form

private void frm_AddNewProdu ct_Load(object sender, System.EventArg s e)

{ ...

DataAccess DataAccess = new DataAccess();

DS_Products = DataAccess.GetP roducts();

.... }

----------------------------

public class DataAccess

{ ....

public DataSet GetProducts()
{
string mySql = "Select itemid, Description, Quantity, Price from
products where producttype = 'P'";
DBConnection myConn = new DBConnection("S QLServer", mySql);
DataSet DS_MyProducts = new DataSet();
DS_MyProducts = myConn.Connecti on();
return DS_MyProducts;
}
}
----------------
public class DBConnection
{
public DataSet MyClientsDS;
public SqlDataAdapter MyDataAdapterCl ients;
string strThisConn, strThisQuery, strTable;
public DBConnection(st ring strDataBase, string strQuery)
{
strThisConn = strDataBase;
strThisQuery = strQuery;
strTable = strMainTable;
}
public DataSet Connection()
{
MyClientsDS = new DataSet();
SqlConnection SQLConn = new SqlConnection(" Data Source=localhos t;
Integrated Security=SSPI;" +
"Initial Catalog=invoici ng");
MyDataAdapterCl ients = new SqlDataAdapter (strThisQuery, SQLConn);
MyDataAdapterCl ients.Fill(MyCl ientsDS);
SQLConn.Close() ;
return MyClientsDS;
}
}

Thanks for your help.
"William Ryan" <do********@nos pam.comcast.net > wrote in message
news:ef******** ******@TK2MSFTN GP09.phx.gbl...
Is it possible to have a Connection class that opens up connection to my DB
and leaves it open until I decide to close it?


In many instances, if you are using any of the 'connected' ado.net

objects, you don't have a choice, you have to open them manually and close them when you are done which is determined by you.
I run many SQL statements so I was wondering if it's possible to call a
Class1.GetProdu cts that sets up an SQL statement and then calls another
Class2.Connect that actually opens up the connection Class2.Connect would not receive anything (since the connections string is always the same) and would not return anything.
Yes, you can do that, but if I understand you correctly, I don't

understand what you are getting by it. You can define a static property connection
somewhere and instantiate it in your class locally each time you need it.
Leaving a connection open just for leaving it's open sake is kind of
antithetical to everything ADO.NET, and unless you are firing A LOT of
sequential statements, I don't know that there's much value if the
connection string remains identical and everything is sitting tin the pool.
If you could describe your ultimate goal, I could probably be of more
assistance, but for the time being, I think theanswer is Yes to both of your questions.

Cheers,

Bill
"Jim" <NA> wrote in message news:uI******** ******@TK2MSFTN GP11.phx.gbl...

Thanks.


Nov 15 '05 #3

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

Similar topics

11
2438
by: pradeep_TP | last post by:
Hi all, I have a few questions that I have been wanting to ask for long. These are all related to ADO.net and specifically to conenction to database. 1) If I have opened a connection to a database through Connection.open() method, and I do not use Connection.close() method, will garbage collector collect the connection object just because i am not using it any more. 2) I am using a dataset, in which i make some modifications to the...
35
11422
by: Eric Sabine | last post by:
In my Finally block, I was using cn.close (where cn is an ADO.NET connection object, SQLConnection to be exact) and then I came across the following in some microsoft code. If Not cn Is Nothing Then CType(cn, IDisposable).Dispose() End If I have to admit, I'm not sure what happens here. Will someone explain this line of code (the middle one, not the if statement LOL) to me please?
5
1467
by: Brian Kitt | last post by:
I wanted to post this on the SQL newsgroup, but I get a 'page not found' when trying to go to that newsgroup. Since it is a C# app, I'll try it here. I have a C# web application (.NET 1.1). I multi-thread requests to 10 different tables (full text indexed tables) on the same database. These are done concurrently, not consecutively. In order to get this to work, I have to open 10 connections to the database. According to the...
7
2477
by: Lau Lei Cheong | last post by:
Hello, Actually I think I should have had asked it long before, but somehow I haven't. Here's the scenerio: Say we have a few pages in an ASP.NET project, each of them needs to connect to the database(Does not really matter but, in case you need to know, either MSSQL or ODBC ones) a few times when it loads to do the CURD things. How there's 3 ways proposed to do so:
14
3507
by: WebMatrix | last post by:
Hello, I have developed a web application that connects to 2 different database servers. The connection strings with db username + password are stored in web.config file. After a code review, one developer suggested that it's a security flaw; therefore connection strings should be kept somewhere else or encrypted. My argument is that web.config file is protected by IIS and Windows security which is the case. And another argument is that...
35
4851
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 string in the web.config. I created a class with a static database connection and the class opens and closes the database.
16
2876
by: crbd98 | last post by:
Hello All, Some time ago, I implemented a data access layer that included a simple connectin pool. At the time, I did it all by myself: I created N connections, each connection associated with a worker thread that would execute the db commands. The pool was fixed and all the connections were created when the db access class was instantiated. The connections remained opened during the whole execution. If a connection was not available...
0
789
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted by the software in your host machine. Stack Trace at System.Net.Sockets.NetworkStream.Write(Byte buffer, Int32 offset, Int32
20
3301
by: fniles | last post by:
I am using VS2003 and connecting to MS Access database. When using a connection pooling (every time I open the OLEDBCONNECTION I use the exact matching connection string), 1. how can I know how many connection has been used ? 2. If the maximum pool size has been reached, what happens when I call the method Open to open the connection ? Will I get an error ? MSDN says the request is queued, but will I get an error in the open method ? ...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10438
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7540
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.