static data access

N

Nomadass

hi,
I have a static class for data access. what if i
declare some private attribute like this.
private static constr =
ConfigurationSettings.ConnectionStrings["mydata"].ConnectionString;

private sqlconnection con = new sqlconnection(constr);

Does these two statements mean that the connection is always open?

thanks in advance.
 
M

Michael Tkachev

Never use the static connection.

You should use just static property that returns connction string.

public class test
{
public static string ConnectionString
{
get
{
return ConfigurationSettings.AppSettings["your key"];
}
}
}

public class con
{

public void aaa()
{
SqlConnection con = new SqlConnection(test.ConnectionString)

....
con.Open();
...
con.Close();
}
}
 
N

Nomada ss

thank you very much for your reply.

now I have another problem about design.

I follow the three tiers desgin. presentation tier.
business tier. and data tier.

As I am develop using C#, ASP.net. sqlServer 2005.
In the data tier i have three layers.
at the buttom is the data store. right above it is
procedures. as it provides an hidden of queries changes
from the layer above. the data access layer which is written in C# to
access
data.

My problem is in the design of this data access layer.
since all my connection to sqlserver. so the connection
are all the same. and command are all stored procedure type.
which means there should be a shared property in this layer.
that is why I have a static connection which can be used by
every connection to database. in such case. i need to serialize the
connection open() method. which is not making
use of the sqlserver can have sever connection opens at the same time.
sqlsever action does not really open a new connection for every query.
if the connection string are the same. so at the user side we may open
more connections then the sqlserver. this is also a kind of waste. in
another words.
we open many connections at the user side, but the sqlserver side only
have several connecitons to map this.

to solver this problem. I propose a design that, at coding side. client
of sqlserver, I construct two arrays or queues.
on is for storing the connection are being used, another for the
conneciton currently free. if a new connection is needed, I check the
free queue first to see whether any idle connectin there, if it is, use
it, otherwise, declare a new one, enqueue to the busy queue. is this
method worthy or sounds reasonable? or i am just try to structure
something useless

I know this description is rather vagour, if you do get it.
please give me suggestion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top