1

SQL Server 2005 SP2で問題が発生し、Windowsフォームとその上にボタンを作成し、次の手順を実行しました。

  1. SQLサービスが実行されていることを確認してから、ボタンをクリックしてください。すべて問題ありません。
  2. SQLサービスを停止してから、もう一度ボタンをクリックします。私のマシンでは、LINE 1のコードで例外は発生せず、 LINE 2で例外が発生しました。これは、例外情報です。

メッセージ:サーバーにリクエストを送信するときに、トランスポートレベルのエラーが発生しました。(プロバイダー:共有メモリプロバイダー、エラー:0-パイプのもう一方の端にプロセスがありません。)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ReconnectSQL
{
    public partial class Form1 : Form
    {

        private string m_ConnectionString = @"Server=(local); Database=testDB; User ID=sa; Password=admins; Connection Timeout=15";

        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        public DataTable GetByFillDataTable()
        {
            try
            {
                SqlCommand cmd = new SqlCommand("getalldata");
                cmd.CommandType = CommandType.StoredProcedure;

                DataTable dt = this.GetDataTable(cmd);
                return dt;
            }
            catch
            {
                throw;
            }
        }


        #region common funcs
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        private DataTable GetDataTable(SqlCommand cmd)
        {
            DataTable dt = new DataTable();

            using (SqlConnection conn = new SqlConnection(this.m_ConnectionString))
            {
                try
                {
                    conn.Open();  // LINE 1
                }
                catch (Exception eX)
                {
                    throw;
                }

                using (SqlDataAdapter adapter = new SqlDataAdapter())
                {
                    try
                    {
                        cmd.Connection = conn;
                        cmd.CommandTimeout = conn.ConnectionTimeout;
                        adapter.SelectCommand = cmd;
                        adapter.Fill(dt);  // LINE 2
                    }
                    catch (Exception eX)
                    {
                        throw;
                    }
                }
            }

            return dt;
        }

        #endregion       

        private void button2_Click(object sender, EventArgs e)
        {

            try
            {
                DataTable dt = GetByFillDataTable();
                listBox1.Items.Add("GetByFillDataTable is called without exceptions!");
            }
            catch (Exception ex)
            {
                listBox1.Items.Add(ex.Message);
            }            }
    }
}

詳細な例外情報:

-       [System.Data.SqlClient.SqlException]    {"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.)"} System.Data.SqlClient.SqlException
+       base    {"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.)"} System.Data.Common.DbException {System.Data.SqlClient.SqlException}
        Class   20  byte
+       Errors  {System.Data.SqlClient.SqlErrorCollection}  System.Data.SqlClient.SqlErrorCollection
        LineNumber  0   int
        Number  233 int
        Procedure   null    string
        Server  "(local)"   string
        Source  ".Net SqlClient Data Provider"  string
        State   0   byte

スタックトレース

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParserStateObject.WriteSni()
   at System.Data.SqlClient.TdsParserStateObject.WritePacket(Byte flushMode)
   at System.Data.SqlClient.TdsParserStateObject.ExecuteFlush()
   at System.Data.SqlClient.TdsParser.TdsExecuteRPC(_SqlRPC[] rpcArray, Int32 timeout, Boolean inSchema, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj, Boolean isCommandProc)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)
   at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)
   at ReconnectSQL.Form1.GetDataTable(SqlCommand cmd) in E:\_public_\sqlFail\ReconnectSQL\ReconnectSQL\Form1.cs:line 138
4

1 に答える 1

1

いくつかの調査の結果、SQLが停止しても接続は接続プールに残っているようです。そのため、SQLを開始し、conn.Open()を呼び出すことで、無効なはずの接続オブジェクトをプールから取得し、次にSqlDataAdapter.Fillを取得します。例外を引き起こす

http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/99963999-a59b-4614-a1b9-869c6dff921e

于 2010-11-26T01:52:06.860 に答える