Posted By: turrican | Sep 9th @ 9:37 PM
page 1 of 1
Comments: 7 | Views: 925
turrican
turrican
Condemnation without investigation is the height of ignorance! - Albert Einstein

I got a WCF service which connects to DB and gets|sets some data via a WCF client. I only do this "SqlConnection sqlConn = new SqlConnection(..." once in the constructor of my class and then I do several SqlCommand MyCommand = sqlConn.CreateCommand(); sometimes at the same time.

Now my question is : Bare in mind several queries are run at the same time by one and|or more client(s), as in if I have a "void listmystuff(string tableID);" it can be called several times from one client or sometimes from different clients but with different "tableID" variable. Do I need to do a "new SqlConnection(..." for each of my queries? I mean, Do I need to put the "new SqlConnection(..." inside my void or is it enough to put it ONCE in the constructor of my class? Also : My class gets created only ONCE as ONE object for each client but it gets used several times ( still same object ) and at the same time sometimes but different "tableID".

I hope I managed to ask this clear enough. My issue is I get a weird error sometimes that sais "Some field is not part of my table" so I was thinking perhaps since I do the "new SqlConnection(..." only once and using the same SqlConnection "everywhere", maybe they get mixed up or something?

Thank you.

Sven Groot
Sven Groot
You can't have everything; after all, where would you put it?
You should be able to use the single connection object. In fact, if you use multiple objects in rapid succession they'll use the same connection anyway due to connection pooling.

Do not however keep the connection alive while the app isn't actually using the database. Open the connection, do whatever commands you have to, then close it as soon as possible.
jugatsu
jugatsu
Boomer zuner

using(SqlConnection sqlConn = new SqlConnection(...))
{
    <your query>
}

Assures the SqlConnection is closed and disposed automatically no matter how the code block is exited.

Sven Groot
Sven Groot
You can't have everything; after all, where would you put it?

Although, My main question is if I just use "one" connection, will the queries get mixed up giving me wrong results?

No, they shouldn't. EDIT: Unless you're using the same SqlConnection object across more than one thread? I'm not entirely sure but I think that's not safe.

There is no real performance hit from recreating the connection object because ADO.NET uses connection pooling so it reuses the actual connection to the database if possible even when you create a new SqlConnection object.

jugatsu
jugatsu
Boomer zuner
Yes, the using statement goes inside your method, i.e.:

public void YourMethod()
{
    using(SqlConnection sqlConn = new SqlConnection(...))
    {
        <your code>
    }
}

Or you can declare the object outside the method (or inside, but before the using statement) and initialize it in the using statement.

You can do this with any class that implements IDisposable.
page 1 of 1
Comments: 7 | Views: 925