Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Sunday, March 25, 2012

Connection pooling issues!

I've got a function that returns a connection to a DataReader:

static public SqlDataReader GetDR(string sql,string connString){SqlConnection myConn =new SqlConnection(connString);SqlCommand cmd =new SqlCommand(sql, myConn);SqlDataReader dr;myConn.Open();dr = cmd.ExecuteReader();return dr;}

I call it like so:

SqlDataReader dr = PAS.GetSqlDataReader.GetDR(sql, connString);

And I'm always careful to close the connection when I'm done using it via dr.Close(). Except I just started noticing that when I run my app and
start doing a fair amount of work around it - I very quickly get a timeout that I've run out of connections. D'oh! I've triple checked my code and
verified that Ialways remember to close my connections. It's there -every single time!

So I'm obviously missing something. Should I not be using a function like GetDR since it doesn't close the connection within the function? 

closing a reader does not automatically close the connection.

dr.close() simply closes the reader leaving the connection open.

if you want to close the connection automatically when the reader is closed, you need to use an overload to the ExecuteReader method

 dr = cmd.ExecuteReader(Data.CommandBehavior.CloseConnection);
http://msdn2.microsoft.com/en-us/library/y6wy5a0f.aspx|||What's the proper way to close the connection then?|||

Am I correct that this opens the connection:

SqlConnection myConn = new SqlConnection(connString);

And this closes it:

myConn.Dispose();

I guess my problem is that my GetDR function returns a DataReader leaving me no access to close myConn. Anything I can do about that?

|||

For some reason, I totally looked over your piece of code to close the connection when the reader is closed. It's exactly what I needed (obviously). Thanks for your help. Seems to be working great now!

mbanavige:

closing a reader does not automatically close the connection.

dr.close() simply closes the reader leaving the connection open.

if you want to close the connection automatically when the reader is closed, you need to use an overload to the ExecuteReader method

 dr = cmd.ExecuteReader(Data.CommandBehavior.CloseConnection);
http://msdn2.microsoft.com/en-us/library/y6wy5a0f.aspx

Saturday, February 25, 2012

connection error

Hello freinds,

i want to copy my bulk files to database using dts.bulkinsert function.For that i need to activate MS Distributed Transatction Coordinator.But whwn i am activating this i am getting [DBNETLIB][ConnectionOpen (Connect()).]SQL Server does not exist or access denied. error. can anyone please suggest me whats d problem exactly and hoe can i remove it.:confused:Hi,

Please check whether the services are started or not.

If not open service manager and start the services.

Regards,
Shashidhar

Sunday, February 19, 2012

Connection - MSSQL - HOST_ID

Hi Everyone,
I wonder if somebody knows what sets the value resulting from the HOST_ID()
function in SQL2000.
It seems that the OleDb connection sets that property (and pass it to the
server when setting up the connection).
My problem is that I need to create 2 connections in my application to the
same database/server using the same credentials, but I need both connections
to return different HOST_ID().
I haven't been able to accomplish that, currently my both, different
connections return the same HOST_ID() when querying the database.
Any ideas?
Thanks, Jos Araujo.You can specify the desired host name using the 'Workstation ID' connection
string keyword or OLEDB property:
Provider=SQLOLEDB;Data Source=MyServer;Initial Catalog=MyDatabase;Integrated
Security=SSPI;Workstation ID=MyHost"
Hope this helps.
Dan Guzman
SQL Server MVP
"Jos Araujo" <josea@.mcrinc.com> wrote in message
news:O3f60VwQGHA.5808@.TK2MSFTNGP12.phx.gbl...
> Hi Everyone,
> I wonder if somebody knows what sets the value resulting from the
> HOST_ID()
> function in SQL2000.
> It seems that the OleDb connection sets that property (and pass it to the
> server when setting up the connection).
> My problem is that I need to create 2 connections in my application to the
> same database/server using the same credentials, but I need both
> connections
> to return different HOST_ID().
> I haven't been able to accomplish that, currently my both, different
> connections return the same HOST_ID() when querying the database.
> Any ideas?
> Thanks, Jos Araujo.
>
>
>|||It is not quite exactly what I wanted: I wanted to change the HOST_ID()
return value, it changes the HOST_NAME() return value.
However: I can workaround my problem by using this.
Thanks! Jos.
"Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> wrote in message
news:O1mkLmyQGHA.5468@.TK2MSFTNGP14.phx.gbl...
> You can specify the desired host name using the 'Workstation ID'
> connection string keyword or OLEDB property:
> Provider=SQLOLEDB;Data Source=MyServer;Initial
> Catalog=MyDatabase;Integrated Security=SSPI;Workstation ID=MyHost"
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "Jos Araujo" <josea@.mcrinc.com> wrote in message
> news:O3f60VwQGHA.5808@.TK2MSFTNGP12.phx.gbl...
>|||I'm glad my answer helped, even though it wasn't exactly what you were
looking for :-)
Dan Guzman
SQL Server MVP
"Jos Araujo" <josea@.mcrinc.com> wrote in message
news:evtB6U4QGHA.1576@.tk2msftngp13.phx.gbl...
> It is not quite exactly what I wanted: I wanted to change the HOST_ID()
> return value, it changes the HOST_NAME() return value.
> However: I can workaround my problem by using this.
> Thanks! Jos.
> "Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> wrote in message
> news:O1mkLmyQGHA.5468@.TK2MSFTNGP14.phx.gbl...
>