public SqlConnection GetCon()
        {
            SqlConnection.ClearAllPools();
            Con = new SqlConnection(ConnectionString);
         
            Con.Open();
          
            return Con;
 for (int i = 0; i < 5000  ; i++)
            {
                        string P_Str_cmdtxt = "Select count(*) from MokuaiGFJStock";                   SqlCommand cmd = new SqlCommand(P_Str_cmdtxt,Sql.GetCon());
                   count=(int)cmd.ExecuteScalar();
                   cmd.Dispose();
                   
             
                Console.WriteLine(i.ToString() + " ");
            }               Console.Write( "结束 ");为何运行2000多次的时候,提示数据库连接错误。
 string ConnectionString = "Max Pool Size=10; Data Source=10.10.16.17;Initial Catalog=PlanInventoryBook_new;uid=sa;pwd=xujunqq1";
        
错误内容:“成功与服务器建立连接,但是在登录前的握手期间发生错误。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 0 - 管道的另一端上无任何进程。)”

解决方案 »

  1.   

    数据库连接要及时关闭,多用using。
    如:using(SqlConnection conn = new Sqlconnection())
    {
      //
      conn.Open();
      //下面再写SqlCommand以及执行语句等
    }
      

  2.   

    SqlConnection 只有Open,没有Close.另外,Connection是可以重用的,没必要每个Command开新的连接。
      

  3.   


    SqlConnection conn = GetCon();
     for (int i = 0; i < 5000  ; i++)
                {
                            string P_Str_cmdtxt = "Select count(*) from MokuaiGFJStock";                   SqlCommand cmd = new SqlCommand(P_Str_cmdtxt,conn);
                       count=(int)cmd.ExecuteScalar();
                       cmd.Dispose();
                       
                 
                    Console.WriteLine(i.ToString() + " ");
                }
                   conn.Close();
                   Console.Write( "结束 ");
      

  4.   

    你这是要整哪样,开5000个sql连接,你想把数据库服务器搞崩溃么,
    每次连接前 判断数据库 连接是否开启,
    如果开启就没必要再创建新的连接了
      

  5.   

      public class SqlHelp
        {
            SqlDataAdapter Da;
            DataSet Ds;
            SqlCommand Cmd;
           string ConnectionString = "Max Pool Size=10; Data Source=10.10.16.17;Initial Catalog=PlanInventoryBook_new;uid=sa;pwd=xujunqq1";
            SqlConnection Con;
            
            /// <summary>
            /// 构造函数
            /// </summary>
            public SqlHelp()
            {
            }        /// <summary>
            /// 连接数据库
            /// </summary>
            /// <returns></returns>
            public SqlConnection GetCon()
            {
                   SqlConnection.ClearAllPools();
                    Con = new SqlConnection(ConnectionString);
                    Con.Close();
                    Con.Open();
                      return Con;
                 
              
                      }
            /// <summary>
            /// 执行SQL语句
            /// </summary>
            /// <param name="cmdtxt">要执行的SQL语句</param>
            /// <returns></returns>
            public bool GetExecute(string cmdtxt)
            {
                Cmd = new SqlCommand(cmdtxt, GetCon());
                try
                {
                    Cmd.ExecuteNonQuery();
                   
                    return true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "错误提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return false;
                }
                finally
                {
                    if (GetCon().State == ConnectionState.Open)
                    {
                        GetCon().Close();
                        GetCon().Dispose();
                    }
                    
                    
                    
                    
                      Cmd.Dispose();
                      Cmd.Connection.Close();
                      SqlConnection.ClearAllPools();
                    
                   // 
                }
                        }
            /// <summary>
            /// 返回数据集
            /// </summary>
            /// <param name="cmdtxt">需要查询的SQL语句</param>
            /// <returns></returns>
            public DataSet GetDs(string cmdtxt)
            {
                try
                {
                    Da = new SqlDataAdapter(cmdtxt, GetCon());
                    Ds = new DataSet();
                    Da.Fill(Ds);
                    return Ds;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message, "错误提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                    return null;
                }
                finally
                {
                     //  Ds.Dispose();
                      SqlConnection.ClearAllPools();
                   
                }
            }
    每次都要调用geton(),数据库连接总被打开后出错。怎么重写啊?
      

  6.   


                        GetCon().Close();
                        GetCon().Dispose();严重错误!你关闭的是新建的连接,而不是原来是用的那个。