当前位置: 代码迷 >> C# >> 超时时间已到,但是尚未从池中获取链接。求解!该如何解决
  详细解决方案

超时时间已到,但是尚未从池中获取链接。求解!该如何解决

热度:324   发布时间:2016-05-05 05:31:42.0
超时时间已到,但是尚未从池中获取链接。求解!
private void button6_Click(object sender, EventArgs e)
        {
            int a = dgvAreaInfo.Rows.Count; 
            for (int b = 0; b < a; b++)
            {
                //获取内部工票价格
                try
                {
                    SqlDataReader sqlread = boperate.getread("SELECT sum(gpjg) as gpjg FROM tb_gpdd where '" + riqistart.Text.Trim() + "' <= ccrq and ccrq <= '" + riqiend.Text.Trim() + "' and cph='" + Convert.ToString(dgvAreaInfo.Rows[b].Cells[0].Value).Trim() + "' and jsfs = '内部用车' ");
                    if (sqlread.Read())
                    {
                        string zhi1 = sqlread["gpjg"].ToString();
                        this.dgvAreaInfo.Rows[b].Cells[5].Value = zhi1.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                }

                //获取内部客车补助
                try
                {
                    SqlDataReader sqlread = boperate.getread("SELECT sum(kcbz) as kcbz FROM tb_gpdd where '" + riqistart.Text.Trim() + "' <= ccrq and ccrq <= '" + riqiend.Text.Trim() + "' and cph='" + Convert.ToString(dgvAreaInfo.Rows[b].Cells[0].Value).Trim() + "' and jsfs = '内部用车' ");
                    if (sqlread.Read())
                    {
                        string zhi2 = sqlread["kcbz"].ToString();
                        this.dgvAreaInfo.Rows[b].Cells[6].Value = zhi2.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                }

                //获取内部货车补助
                try
                {
                    SqlDataReader sqlread = boperate.getread("SELECT sum(buzhu) as buzhu FROM tb_gpdd where '" + riqistart.Text.Trim() + "' <= ccrq and ccrq <= '" + riqiend.Text.Trim() + "' and cph='" + Convert.ToString(dgvAreaInfo.Rows[b].Cells[0].Value).Trim() + "' and jsfs = '内部用车' ");
                    if (sqlread.Read())
                    {
                        string zhi3 = sqlread["buzhu"].ToString();
                        this.dgvAreaInfo.Rows[b].Cells[7].Value = zhi3.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                }

                //获取内部过路票
                try
                {
                    SqlDataReader sqlread = boperate.getread("SELECT sum(glp) as glp FROM tb_gpdd where '" + riqistart.Text.Trim() + "' <= ccrq and ccrq <= '" + riqiend.Text.Trim() + "' and cph='" + Convert.ToString(dgvAreaInfo.Rows[b].Cells[0].Value).Trim() + "' and jsfs = '内部用车' ");
                    if (sqlread.Read())
                    {
                        string zhi4 = sqlread["glp"].ToString();
                        this.dgvAreaInfo.Rows[b].Cells[8].Value = zhi4.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                }

                //获取现金工票
                try
                {
                    SqlDataReader sqlread = boperate.getread("SELECT sum(gpjg) as xjgpjg FROM tb_gpdd where '" + riqistart.Text.Trim() + "' <= ccrq and ccrq <= '" + riqiend.Text.Trim() + "' and cph='" + Convert.ToString(dgvAreaInfo.Rows[b].Cells[0].Value).Trim() + "' and jsfs = '现金' ");
                    if (sqlread.Read())
                    {
                        string zhi5 = sqlread["xjgpjg"].ToString();
                        this.dgvAreaInfo.Rows[b].Cells[9].Value = zhi5.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                }

                //获取现金客车补助
                try
                {
                    SqlDataReader sqlread = boperate.getread("SELECT sum(kcbz) as xjkcbz FROM tb_gpdd where '" + riqistart.Text.Trim() + "' <= ccrq and ccrq <= '" + riqiend.Text.Trim() + "' and cph='" + Convert.ToString(dgvAreaInfo.Rows[b].Cells[0].Value).Trim() + "' and jsfs = '现金' ");
                    if (sqlread.Read())
                    {
                        string zhi6 = sqlread["xjkcbz"].ToString();
                        this.dgvAreaInfo.Rows[b].Cells[10].Value = zhi6.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                }

                //获取现金货车补助
                try
                {
                    SqlDataReader sqlread = boperate.getread("SELECT sum(buzhu) as xjbuzhu FROM tb_gpdd where '" + riqistart.Text.Trim() + "' <= ccrq and ccrq <= '" + riqiend.Text.Trim() + "' and cph='" + Convert.ToString(dgvAreaInfo.Rows[b].Cells[0].Value).Trim() + "' and jsfs = '现金' ");
                    if (sqlread.Read())
                    {
                        string zhi7 = sqlread["xjbuzhu"].ToString();
                        this.dgvAreaInfo.Rows[b].Cells[11].Value = zhi7.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                }

                //获取现金过路票
                try
                {
                    SqlDataReader sqlread = boperate.getread("SELECT sum(glp) as xjglp FROM tb_gpdd where '" + riqistart.Text.Trim() + "' <= ccrq and ccrq <= '" + riqiend.Text.Trim() + "' and cph='" + Convert.ToString(dgvAreaInfo.Rows[b].Cells[0].Value).Trim() + "' and jsfs = '现金' ");
                    if (sqlread.Read())
                    {
                        string zhi8 = sqlread["xjglp"].ToString();
                        this.dgvAreaInfo.Rows[b].Cells[12].Value = zhi8.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                }
            }
        }


自定义DGV的列,将数据显示在不同列中,出现的错误提示。

------解决思路----------------------
对于返回 DbDataReader 的 SqlHelper 方法,我们一直是反对的。因为使用者很容易就忘记(甚至根本不懂)需要及时通过它关闭所占用的数据库逻辑连接。而不及时释放逻辑连接,那么数据库连接池就会很快用尽。

所以好的 SqlHelper 返回的是查询对象集合,例如 List<T> 类型的,一旦你取出数据集合,那么数据库逻辑连接立刻就关闭了。及时关闭数据库连接,才能保证数据库连接池不会用尽。
  相关解决方案