Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Problem on first execution to SQLServer table

Reply
 
Old 10-05-2007   #1 (permalink)
Frank


 
 

Problem on first execution to SQLServer table

Hi,

I have a script which gathers information from a SQLServer table but it
errors on the first execution. On subsequent calls, there is no errors and
executes fine. I think this may be due to a process which starts and listens
for other calls from SQLServer. Does anyone know how to resolve this? Here
is the script:

$DB = "server=sqlhost;integrated security=true;database=test"
$conn = new-object System.Data.SqlClient.SqlConnection
$conn.ConnectionString = $DB
$conn.Open()
$cmd = new-object System.Data.SqlClient.SqlCommand
$cmd.Connection=$conn

$cmd.CommandText="
SELECT * from notes"
$rdr = $cmd.ExecuteReader()
While($rdr.Read()){
write-output "$($rdr['notes_id'])"
write-output "$($rdr['notes'])"
}
$conn.Close()
=============================================

Here is the error:

[D:\scp]: ./f.ps1
Exception calling "ExecuteReader" with "0" argument(s): "A transport-level
error has occurred when sending the request to the server. (provider: TCP
Provider, error: 0 - An existing connection was forcibly closed by the remote
host.)"
At D:\scp\f.ps1:13 char:26
+ $rdr = $cmd.ExecuteReader( <<<< )
You cannot call a method on a null-valued expression.
At D:\scp\f.ps1:15 char:16
+ While($rdr.Read( <<<< )){
==============================

Thanks in advance,



My System SpecsSystem Spec
Old 10-06-2007   #2 (permalink)
RichS


 
 

RE: Problem on first execution to SQLServer table

It doesn't seem to be your script as you have a $conn.Close() in it. What
else do you have running that could have an open connection?
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Frank" wrote:
Quote:

> Hi,
>
> I have a script which gathers information from a SQLServer table but it
> errors on the first execution. On subsequent calls, there is no errors and
> executes fine. I think this may be due to a process which starts and listens
> for other calls from SQLServer. Does anyone know how to resolve this? Here
> is the script:
>
> $DB = "server=sqlhost;integrated security=true;database=test"
> $conn = new-object System.Data.SqlClient.SqlConnection
> $conn.ConnectionString = $DB
> $conn.Open()
> $cmd = new-object System.Data.SqlClient.SqlCommand
> $cmd.Connection=$conn
>
> $cmd.CommandText="
> SELECT * from notes"
> $rdr = $cmd.ExecuteReader()
> While($rdr.Read()){
> write-output "$($rdr['notes_id'])"
> write-output "$($rdr['notes'])"
> }
> $conn.Close()
> =============================================
>
> Here is the error:
>
> [D:\scp]: ./f.ps1
> Exception calling "ExecuteReader" with "0" argument(s): "A transport-level
> error has occurred when sending the request to the server. (provider: TCP
> Provider, error: 0 - An existing connection was forcibly closed by the remote
> host.)"
> At D:\scp\f.ps1:13 char:26
> + $rdr = $cmd.ExecuteReader( <<<< )
> You cannot call a method on a null-valued expression.
> At D:\scp\f.ps1:15 char:16
> + While($rdr.Read( <<<< )){
> ==============================
>
> Thanks in advance,
>
>
My System SpecsSystem Spec
Old 10-06-2007   #3 (permalink)
Frank


 
 

RE: Problem on first execution to SQLServer table

I guess I don't really understand the question. When I first run this
script, I get the error below. When I rerun this script, I don't get the
error. Before I do the first execution of this script, I do a "sp_who2" in
SQLServer and kill all processes that has the DB open. Does that help?

Thanks,



"RichS" wrote:
Quote:

> It doesn't seem to be your script as you have a $conn.Close() in it. What
> else do you have running that could have an open connection?
> --
> Richard Siddaway
> Please note that all scripts are supplied "as is" and with no warranty
> Blog: http://richardsiddaway.spaces.live.com/
> PowerShell User Group: http://www.get-psuguk.org.uk
>
>
> "Frank" wrote:
>
Quote:

> > Hi,
> >
> > I have a script which gathers information from a SQLServer table but it
> > errors on the first execution. On subsequent calls, there is no errors and
> > executes fine. I think this may be due to a process which starts and listens
> > for other calls from SQLServer. Does anyone know how to resolve this? Here
> > is the script:
> >
> > $DB = "server=sqlhost;integrated security=true;database=test"
> > $conn = new-object System.Data.SqlClient.SqlConnection
> > $conn.ConnectionString = $DB
> > $conn.Open()
> > $cmd = new-object System.Data.SqlClient.SqlCommand
> > $cmd.Connection=$conn
> >
> > $cmd.CommandText="
> > SELECT * from notes"
> > $rdr = $cmd.ExecuteReader()
> > While($rdr.Read()){
> > write-output "$($rdr['notes_id'])"
> > write-output "$($rdr['notes'])"
> > }
> > $conn.Close()
> > =============================================
> >
> > Here is the error:
> >
> > [D:\scp]: ./f.ps1
> > Exception calling "ExecuteReader" with "0" argument(s): "A transport-level
> > error has occurred when sending the request to the server. (provider: TCP
> > Provider, error: 0 - An existing connection was forcibly closed by the remote
> > host.)"
> > At D:\scp\f.ps1:13 char:26
> > + $rdr = $cmd.ExecuteReader( <<<< )
> > You cannot call a method on a null-valued expression.
> > At D:\scp\f.ps1:15 char:16
> > + While($rdr.Read( <<<< )){
> > ==============================
> >
> > Thanks in advance,
> >
> >
My System SpecsSystem Spec
Old 10-07-2007   #4 (permalink)
RichS


 
 

RE: Problem on first execution to SQLServer table

Do you have SQL management studio or anything open?? I was wondering if
there was a connection there that could be causing your problem.

The error message you are getting is related to the connection and as you
have an explicit close() on the connection its not your script connection.

Reason I'm asking is that I've not been able to duplicate the error message
& was trying to figure out what else could be causing it
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Frank" wrote:
Quote:

> I guess I don't really understand the question. When I first run this
> script, I get the error below. When I rerun this script, I don't get the
> error. Before I do the first execution of this script, I do a "sp_who2" in
> SQLServer and kill all processes that has the DB open. Does that help?
>
> Thanks,
>
>
>
> "RichS" wrote:
>
Quote:

> > It doesn't seem to be your script as you have a $conn.Close() in it. What
> > else do you have running that could have an open connection?
> > --
> > Richard Siddaway
> > Please note that all scripts are supplied "as is" and with no warranty
> > Blog: http://richardsiddaway.spaces.live.com/
> > PowerShell User Group: http://www.get-psuguk.org.uk
> >
> >
> > "Frank" wrote:
> >
Quote:

> > > Hi,
> > >
> > > I have a script which gathers information from a SQLServer table but it
> > > errors on the first execution. On subsequent calls, there is no errors and
> > > executes fine. I think this may be due to a process which starts and listens
> > > for other calls from SQLServer. Does anyone know how to resolve this? Here
> > > is the script:
> > >
> > > $DB = "server=sqlhost;integrated security=true;database=test"
> > > $conn = new-object System.Data.SqlClient.SqlConnection
> > > $conn.ConnectionString = $DB
> > > $conn.Open()
> > > $cmd = new-object System.Data.SqlClient.SqlCommand
> > > $cmd.Connection=$conn
> > >
> > > $cmd.CommandText="
> > > SELECT * from notes"
> > > $rdr = $cmd.ExecuteReader()
> > > While($rdr.Read()){
> > > write-output "$($rdr['notes_id'])"
> > > write-output "$($rdr['notes'])"
> > > }
> > > $conn.Close()
> > > =============================================
> > >
> > > Here is the error:
> > >
> > > [D:\scp]: ./f.ps1
> > > Exception calling "ExecuteReader" with "0" argument(s): "A transport-level
> > > error has occurred when sending the request to the server. (provider: TCP
> > > Provider, error: 0 - An existing connection was forcibly closed by the remote
> > > host.)"
> > > At D:\scp\f.ps1:13 char:26
> > > + $rdr = $cmd.ExecuteReader( <<<< )
> > > You cannot call a method on a null-valued expression.
> > > At D:\scp\f.ps1:15 char:16
> > > + While($rdr.Read( <<<< )){
> > > ==============================
> > >
> > > Thanks in advance,
> > >
> > >
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Typed datasets and table table adapters components not showing inToolbox .NET General
how do you create a dynamic table/pivot table .NET General
connect to paradox db from sqlserver database table .NET General
Execution problem PowerShell
Wacom table and sleep problem Vista performance & maintenance


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46