I've the following doubt. I'm trying to Initialize an SQLConnection variable in the following way:
using (SqlConnection cnn = new SqlConnection(_MyConnectionString))
{
if (cnn.State == ConnectionState.Closed)
cnn.Open();
using (SqlBulkCopy objBulkCopy = new SqlBulkCopy(cnn))
{
. . .
}
}
The code above is working OK. I just recently added the if condition after the first using. Without it at the momment to initialize the cnn variable (SQLConnection) I don't know why this variable was initialize with its state property as closed.
That's why I validate the state property and open it in case it's closed.
Based on it, I have the following questions:
Don't it should be Open at the momment to initialize the SQLConnection varaible?
Should I explicit close the SQlConnection now?
... or the using statement do this?
I hope you can help me whit this. Might be those questions are basic however, I haven't facing this.
SqlConnection does not open the connection until you call the Open method. This lets you set other properties on the object after you create it. Also it lets you create the connection object early then wait until it is needed before you open it.
As for closing, yes you should close and Dispose, however the "using" statement is a neat way of handling this as it calls the Dispose for you.
Marked as answer by Lesthad_mk on Apr 30, 2012 03:49 PM
Don't it should be Open at the momment to initialize the SQLConnection varaible?
Should I explicit close the SQlConnection now?
... or the using statement do this?
1. No, you will have to open the connection yourself. It is not opened automatically.
2. You don't need to close it explicitly if you are using a using statement.
3. Yes, the using statement will make sure that the connection is closed and disposed when leaving the using block.
Please 'Mark as Answer' if this post helped you.
Marked as answer by Lesthad_mk on Apr 30, 2012 03:49 PM
Now, I see it. I was a little confused about this, because if I use the connectionString to initialize the SQLBulkcopy variable, it implicit open the connection to the DB.
Lesthad_mk
Member
96 Points
159 Posts
Connection State closed when Initialize an SQLConnection
Apr 30, 2012 03:31 PM|LINK
Hi all
I've the following doubt. I'm trying to Initialize an SQLConnection variable in the following way:
using (SqlConnection cnn = new SqlConnection(_MyConnectionString)) { if (cnn.State == ConnectionState.Closed) cnn.Open(); using (SqlBulkCopy objBulkCopy = new SqlBulkCopy(cnn)) { . . . } }The code above is working OK. I just recently added the if condition after the first using. Without it at the momment to initialize the cnn variable (SQLConnection) I don't know why this variable was initialize with its state property as closed.
That's why I validate the state property and open it in case it's closed.
Based on it, I have the following questions:
I hope you can help me whit this. Might be those questions are basic however, I haven't facing this.
Regards!
AidyF
Star
9204 Points
1570 Posts
Re: Connection State closed when Initialize an SQLConnection
Apr 30, 2012 03:36 PM|LINK
SqlConnection does not open the connection until you call the Open method. This lets you set other properties on the object after you create it. Also it lets you create the connection object early then wait until it is needed before you open it.
As for closing, yes you should close and Dispose, however the "using" statement is a neat way of handling this as it calls the Dispose for you.
mm10
Contributor
6439 Points
1184 Posts
Re: Connection State closed when Initialize an SQLConnection
Apr 30, 2012 03:40 PM|LINK
1. No, you will have to open the connection yourself. It is not opened automatically.
2. You don't need to close it explicitly if you are using a using statement.
3. Yes, the using statement will make sure that the connection is closed and disposed when leaving the using block.
Lesthad_mk
Member
96 Points
159 Posts
Re: Connection State closed when Initialize an SQLConnection
Apr 30, 2012 03:49 PM|LINK
Hi all
Now, I see it. I was a little confused about this, because if I use the connectionString to initialize the SQLBulkcopy variable, it implicit open the connection to the DB.
You right, it should be open done manually.
Thanks and regards!