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

alternative for VB.NET module in c#?

hi there,

in my vb apps, i used to use a module where i stored global variables, but
in c# this doesnt seem to exist, what i want to do is create global
sqlcommand,sqlconnection variables that can be reused from anywhere within
the app.

please advise.
thanks,
Paul
Apr 16 '07 #1
10 8079
This sounds like static properties, e.g.

public static class GlobalData {
public static string PrimaryConnectionString {get;set;} // details
not shown
}

3 additional thoughts:
* You might benefit from using the inbuilt pooling rather than keeping
an explicit SqlConnection open - particularly as complexity increases
(hence I illustrated with a connection STRING not a connection)
* Normal thread-safety rules apply to static members
* Of course, a better option (for connection strings) would might be
to use the ConfigurationManager classes and a config file

Marc
Apr 16 '07 #2
"Marc Gravell" <ma**********@gmail.comwrote in message
news:e3****************@TK2MSFTNGP04.phx.gbl...
* You might benefit from using the inbuilt pooling rather than keeping an
explicit SqlConnection open - particularly as complexity increases (hence
I illustrated with a connection STRING not a connection)
Absolutely. The last thing an application should do is keep a permanently
open link to a database. This is especially true of web apps...
* Of course, a better option (for connection strings) would might be to
use the ConfigurationManager classes and a config file
Definitely.
Apr 16 '07 #3
Thanks for all replies.

My question was based for a Windows.NET c# desktop application, not a web
application so i assume its better to keep the connection open until the
user logs out of the application, or not?

As for the connectionstring, i'm using App.Config to store the string in
there and read it into the app during run-time.

regards,
Paul
"Mark Rae" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
"Marc Gravell" <ma**********@gmail.comwrote in message
news:e3****************@TK2MSFTNGP04.phx.gbl...
>* You might benefit from using the inbuilt pooling rather than keeping an
explicit SqlConnection open - particularly as complexity increases (hence
I illustrated with a connection STRING not a connection)

Absolutely. The last thing an application should do is keep a permanently
open link to a database. This is especially true of web apps...
>* Of course, a better option (for connection strings) would might be to
use the ConfigurationManager classes and a config file

Definitely.

Apr 16 '07 #4
Even in a data-connected winform I would tend to use pooling and
tightly scoped connections. Smarter minds than mine have invested much
time to make pooling work effectively, but more importantly it means I
don't have to worry about issues with 2 parallel blocks of code
attempting to use the same connection at once.

Marc
Apr 16 '07 #5
Thanks for your reply mark.

In my little test app i created the following - a static public class called
DatabaseConnection with public properties "connection" and "command" which i
can access.

What i am playing around with is the following ideas: on each data call i do
the following:

1. call DatabaseConnection.Open();
2. ...here i do the SQL call(s)
3. call DatabaseConnection.Close();

is this an efficient way of programming or would it be better to open the
connection once on application start, then close the connection when the
application ends?

thanks,
Paul
"Marc Gravell" <ma**********@gmail.comwrote in message
news:u$**************@TK2MSFTNGP03.phx.gbl...
Even in a data-connected winform I would tend to use pooling and tightly
scoped connections. Smarter minds than mine have invested much time to
make pooling work effectively, but more importantly it means I don't have
to worry about issues with 2 parallel blocks of code attempting to use the
same connection at once.

Marc

Apr 16 '07 #6
Open and close database connections as quickly as possible. The .Net
platform is architected to work and perform best in this way.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Milsnips" <mi******@hotmail.comwrote in message
news:%2***************@TK2MSFTNGP05.phx.gbl...
Thanks for your reply mark.

In my little test app i created the following - a static public class
called DatabaseConnection with public properties "connection" and
"command" which i can access.

What i am playing around with is the following ideas: on each data call i
do the following:

1. call DatabaseConnection.Open();
2. ...here i do the SQL call(s)
3. call DatabaseConnection.Close();

is this an efficient way of programming or would it be better to open the
connection once on application start, then close the connection when the
application ends?

thanks,
Paul
"Marc Gravell" <ma**********@gmail.comwrote in message
news:u$**************@TK2MSFTNGP03.phx.gbl...
>Even in a data-connected winform I would tend to use pooling and tightly
scoped connections. Smarter minds than mine have invested much time to
make pooling work effectively, but more importantly it means I don't have
to worry about issues with 2 parallel blocks of code attempting to use
the same connection at once.

Marc


Apr 17 '07 #7
on each data call i do the following:

It really comes down to how your code is situated. Generally, the
pattern would be
using(connection = [create]) {
[open]
[do something]
[close]
}

As long as pooling is enabled, this doesn't actually close the
connection; it returns it to the pool. Chances are that next time you
open a connection you will get the same underlying connection
(although almost certainly a different .Net wrapper object).

The "do something" could be a single operation or multiple operations,
but not usually the entire app; the lifetime of a connection should be
tightly scoped as far as possible. Again, I stress that in many common
scenarios you actually end up achieving the same thing - i.e. a single
connection that is re-used, but this approach is usually preferable -
more efficient and less chance of bugs due to re-entrancy / threading
etc (attempting to use the same connection twice at the same time).

Marc

Apr 17 '07 #8
On Apr 17, 11:53 am, "Kevin Spencer" <unclechut...@nothinks.com>
wrote:
Open and close database connections as quickly as possible. The .Net
platform is architected to work and perform best in this way.
This is certainly the case for ASP.NET etc which will make use of the
pooled connections in a useful way.

I doubt that it will actually make any difference on a client app
which would only ever use one connection at a time anyway. I could be
wrong, and I'd be interested to know if so :)

I'd still recommend opening and closing the connections as quickly as
possible anyway, for consistency when changing whether you're coding
on ASP.NET or a thick client.

Jon

Apr 17 '07 #9
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
I'd still recommend opening and closing the connections as quickly as
possible anyway, for consistency when changing whether you're coding
on ASP.NET or a thick client.
Especially if you have a DAL which can just be dropped into either type of
project unchanged... :-)
Apr 17 '07 #10
Use a class

public class DataVariables
{
public DataVariables(string connstr)
{
con=new SqlConnection(connstr);
}
private SqlConnection conn;
public SqlConnection Conn
{
get{
return conn;
}
}
}

etc.

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Apr 18 '07 #11

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

Similar topics

13
by: Drochom | last post by:
Hello, I have a huge problem with loading very simple structure into memory it is a list of tuples, it has 6MB and consists of 100000 elements >import cPickle >plik = open("mealy","r")...
46
by: Robin Becker | last post by:
It seems that the rotor module is being deprecated in 2.3, but there doesn't seem to be an obvious alternative. I'm using it just for obfuscation. It seems we have ssl available in 2.3 for sockets,...
0
by: Daniele Varrazzo | last post by:
If you need a container to look into, there is the sets module that provides a couple of them. If you need a sorted list, there is the bisect module. But i don't think it fits your need for a...
0
by: Dan Gass | last post by:
I've written a Python module (available at https://sourceforge.net/projects/config-py/ see "Home Page" for documentation, "Files" for download) that provides easy access to configuration file...
0
by: Jeremy Sanders | last post by:
I've been installing modules in Unix by using python setup.py install --home=/usr/local/python/python-2.3 In PYTHONPATH I've included the /usr/local/python/python-2.3/lib/python directory...
7
by: Henry Ludemann | last post by:
I've been writing an optparse alternative (using getopt) that is at a stage where I'd be interested in people's opinions. It allows you to easily creating command line interfaces to existing...
14
by: simonwittber | last post by:
I've written a simple module which serializes these python types: IntType, TupleType, StringType, FloatType, LongType, ListType, DictType It available for perusal here: ...
115
by: TheAd | last post by:
At this moment I use MsAccess and i can build about every databound application i want. Who knows about a serious open source alternative? Because Windows will be a client platform for some time, i...
2
by: Adrian Biljan | last post by:
Hey all, I'm trying to create a timer class so I can control when the timer start and stops. The ideas is to have a form with a start button, and a stop button. Start invokes a class method...
6
by: ss.teo | last post by:
I want to fork a new process and my executable binary is stored inside somewhere. Is there any possible way to fork a process using CreateProcess API without writing to the disk first? Like let's...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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...
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.