Conn.Open() still work after SQL Service is stopped

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have met a issue with sql server 2005 SP2, I have created a windows form and with a button on it, and with the following steps:
<ol>
Make sure Sql service is running, then click the button, everything is OK Stop the Sql service, and then click the button again, on my machine, there is not exception at the code of
LINE 1 , exception occurred at LINE 2 , and this is the exception info:
</ol>
Message: A transport-level error has occurred when sending the request to the server. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.)
<pre>
Code:
<span><div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System; 
<span style="color:Blue; using System.Collections.Generic; 
<span style="color:Blue; using System.ComponentModel; 
<span style="color:Blue; using System.Data; 
<span style="color:Blue; using System.Drawing; 
<span style="color:Blue; using System.Text; 
<span style="color:Blue; using System.Windows.Forms; 
<span style="color:Blue; using System.Data.SqlClient; 
 
<span style="color:Blue; namespace ReconnectSQL 
{ 
  <span style="color:Blue; public <span style="color:Blue; partial <span style="color:Blue; class Form1 : Form 
  { 
 
    <span style="color:Blue; private <span style="color:Blue; string m_ConnectionString = <span style="color:#A31515; @"Server=(local); Database=testDB; User ID=sa; Password=admins; Connection Timeout=15"; 
 
    <span style="color:Blue; public Form1() 
    { 
      InitializeComponent(); 
    } 
 
    <span style="color:Gray; /// <span style="color:Gray; <summary><span style="color:Green;  
    <span style="color:Gray; ///<span style="color:Green;  
    <span style="color:Gray; /// <span style="color:Gray; </summary><span style="color:Green;  
    <span style="color:Blue; public DataTable GetByFillDataTable() 
    { 
      <span style="color:Blue; try 
      { 
        SqlCommand cmd = <span style="color:Blue; new SqlCommand(<span style="color:#A31515; "getalldata"); 
        cmd.CommandType = CommandType.StoredProcedure; 
 
        DataTable dt = <span style="color:Blue; this.GetDataTable(cmd); 
        <span style="color:Blue; return dt; 
      } 
      <span style="color:Blue; catch 
      { 
        <span style="color:Blue; throw; 
      } 
    } 
 
 
    <span style="color:Blue; #region common funcs 
    <span style="color:Gray; /// <span style="color:Gray; <summary><span style="color:Green;  
    <span style="color:Gray; ///<span style="color:Green;  
    <span style="color:Gray; /// <span style="color:Gray; </summary><span style="color:Green;  
    <span style="color:Gray; /// <span style="color:Gray; <param name="cmd <span style="color:Gray; </param><span style="color:Green;  
    <span style="color:Gray; /// <span style="color:Gray; <returns><span style="color:Gray; </returns><span style="color:Green;  
    <span style="color:Blue; private DataTable GetDataTable(SqlCommand cmd) 
    { 
      DataTable dt = <span style="color:Blue; new DataTable(); 
 
      <span style="color:Blue; using (SqlConnection conn = <span style="color:Blue; new SqlConnection(<span style="color:Blue; this.m_ConnectionString)) 
      { 
        <span style="color:Blue; try 
        { 
          conn.Open(); <span style="color:Green; // LINE 1 
        } 
        <span style="color:Blue; catch (Exception eX) 
        { 
          <span style="color:Blue; throw; 
        } 
 
        <span style="color:Blue; using (SqlDataAdapter adapter = <span style="color:Blue; new SqlDataAdapter()) 
        { 
          <span style="color:Blue; try 
          { 
            cmd.Connection = conn; 
            cmd.CommandTimeout = conn.ConnectionTimeout; 
            adapter.SelectCommand = cmd; 
            adapter.Fill(dt); <span style="color:Green; // LINE 2 
          } 
          <span style="color:Blue; catch (Exception eX) 
          { 
            <span style="color:Blue; throw; 
          } 
        } 
      } 
 
      <span style="color:Blue; return dt; 
    } 
 
    <span style="color:Blue; #endregion    
 
    <span style="color:Blue; private <span style="color:Blue; void button2_Click(<span style="color:Blue; object sender, EventArgs e) 
    { 
 
      <span style="color:Blue; try 
      { 
        DataTable dt = GetByFillDataTable(); 
        listBox1.Items.Add(<span style="color:#A31515; "GetByFillDataTable is called without exceptions!"); 
      } 
      <span style="color:Blue; catch (Exception ex) 
      { 
        listBox1.Items.Add(ex.Message); 
      }      } 
  } 
}
[/code][/code]
Detailed exception info:
<pre>
Code:
<span>-<span>       <span>[<span>System<span>.<span>Data<span>.<span>SqlClient<span>.<span>SqlException<span>]<span>    <span>{<span>"A transport-level error has occurred when sending the request to the server. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.)"<span>}<span> <span>System<span>.<span>Data<span>.<span>SqlClient<span>.<span>SqlException<span> 
<span>+<span>       <span>base<span>    <span>{<span>"A transport-level error has occurred when sending the request to the server. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.)"<span>}<span> <span>System<span>.<span>Data<span>.<span>Common<span>.<span>DbException<span> <span>{<span>System<span>.<span>Data<span>.<span>SqlClient<span>.<span>SqlException<span>}<span> 
        <span>Class<span>   <span>20<span>  <span>byte<span> 
<span>+<span>       <span>Errors<span>  <span>{<span>System<span>.<span>Data<span>.<span>SqlClient<span>.<span>SqlErrorCollection<span>}<span>  <span>System<span>.<span>Data<span>.<span>SqlClient<span>.<span>SqlErrorCollection<span> 
        <span>LineNumber<span>  <span>0<span>   <span>int<span> 
        <span>Number<span>  <span>233<span> <span>int<span> 
        <span>Procedure<span>   <span>null<span>    <span>string<span> 
        <span>Server<span>  <span>"(local)"<span>   <span>string<span> 
        <span>Source<span>  <span>".Net SqlClient Data Provider"<span>  <span>string<span> 
        <span>State<span>   <span>0<span>   <span>byte<span>
[/code]
StackTrace
<pre>
Code:
<span>at <span>System<span>.<span>Data<span>.<span>SqlClient<span>.<span>SqlConnection<span>.<span>OnError<span>(<span>SqlException<span> exception<span>,<span> <span>Boolean<span> breakConnection<span>)<span> 
   at <span>System<span>.<span>Data<span>.<span>SqlClient<span>.<span>SqlInternalConnection<span>.<span>OnError<span>(<span>SqlException<span> exception<span>,<span> <span>Boolean<span> breakConnection<span>)<span> 
   at <span>System<span>.<span>Data<span>.<span>SqlClient<span>.<span>TdsParser<span>.<span>ThrowExceptionAndWarning<span>(<span>TdsParserStateObject<span> stateObj<span>)<span> 
   at <span>System<span>.<span>Data<span>.<span>SqlClient<span>.<span>TdsParserStateObject<span>.<span>WriteSni<span>()<span> 
   at <span>System<span>.<span>Data<span>.<span>SqlClient<span>.<span>TdsParserStateObject<span>.<span>WritePacket<span>(<span>Byte<span> flushMode<span>)<span> 
   at <span>System<span>.<span>Data<span>.<span>SqlClient<span>.<span>TdsParserStateObject<span>.<span>ExecuteFlush<span>()<span> 
   at <span>System<span>.<span>Data<span>.<span>SqlClient<span>.<span>TdsParser<span>.<span>TdsExecuteRPC<span>(<span>_SqlRPC<span>[]<span> rpcArray<span>,<span> <span>Int32<span> timeout<span>,<span> <span>Boolean<span> inSchema<span>,<span> <span>SqlNotificationRequest<span> notificationRequest<span>,<span> <span>TdsParserStateObject<span> stateObj<span>,<span> <span>Boolean<span> isCommandProc<span>)<span> 
   at <span>System<span>.<span>Data<span>.<span>SqlClient<span>.<span>SqlCommand<span>.<span>RunExecuteReaderTds<span>(<span>CommandBehavior<span> cmdBehavior<span>,<span> <span>RunBehavior<span> runBehavior<span>,<span> <span>Boolean<span> returnStream<span>,<span> <span>Boolean<span> async<span>)<span> 
   at <span>System<span>.<span>Data<span>.<span>SqlClient<span>.<span>SqlCommand<span>.<span>RunExecuteReader<span>(<span>CommandBehavior<span> cmdBehavior<span>,<span> <span>RunBehavior<span> runBehavior<span>,<span> <span>Boolean<span> returnStream<span>,<span> <span>String<span> method<span>,<span> <span>DbAsyncResult<span> result<span>)<span> 
   at <span>System<span>.<span>Data<span>.<span>SqlClient<span>.<span>SqlCommand<span>.<span>RunExecuteReader<span>(<span>CommandBehavior<span> cmdBehavior<span>,<span> <span>RunBehavior<span> runBehavior<span>,<span> <span>Boolean<span> returnStream<span>,<span> <span>String<span> method<span>)<span> 
   at <span>System<span>.<span>Data<span>.<span>SqlClient<span>.<span>SqlCommand<span>.<span>ExecuteReader<span>(<span>CommandBehavior<span> behavior<span>,<span> <span>String<span> method<span>)<span> 
   at <span>System<span>.<span>Data<span>.<span>SqlClient<span>.<span>SqlCommand<span>.<span>ExecuteDbDataReader<span>(<span>CommandBehavior<span> behavior<span>)<span> 
   at <span>System<span>.<span>Data<span>.<span>Common<span>.<span>DbCommand<span>.<span>System<span>.<span>Data<span>.<span>IDbCommand<span>.<span>ExecuteReader<span>(<span>CommandBehavior<span> behavior<span>)<span> 
   at <span>System<span>.<span>Data<span>.<span>Common<span>.<span>DbDataAdapter<span>.<span>FillInternal<span>(<span>DataSet<span> dataset<span>,<span> <span>DataTable<span>[]<span> datatables<span>,<span> <span>Int32<span> startRecord<span>,<span> <span>Int32<span> maxRecords<span>,<span> <span>String<span> srcTable<span>,<span> <span>IDbCommand<span> command<span>,<span> <span>CommandBehavior<span> behavior<span>)<span> 
   at <span>System<span>.<span>Data<span>.<span>Common<span>.<span>DbDataAdapter<span>.<span>Fill<span>(<span>DataTable<span>[]<span> dataTables<span>,<span> <span>Int32<span> startRecord<span>,<span> <span>Int32<span> maxRecords<span>,<span> <span>IDbCommand<span> command<span>,<span> <span>CommandBehavior<span> behavior<span>)<span> 
   at <span>System<span>.<span>Data<span>.<span>Common<span>.<span>DbDataAdapter<span>.<span>Fill<span>(<span>DataTable<span> dataTable<span>)<span> 
   at <span>ReconnectSQL<span>.<span>Form1<span>.<span>GetDataTable<span>(<span>SqlCommand<span> cmd<span>)<span> <span>in<span> E<span>:<span>_public_<span><span>sqlFail<span><span>ReconnectSQL
[/code]

View the full article
 
Back
Top