public SqlConnection GetCon()
        {
            try
            {
                string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "参数配置.xml");
                if (File.Exists(configFile))
                {
                    XmlOperator xmlOperator = new XmlOperator(configFile);
                    int size = xmlOperator.GetXmlReader().Length;
                    string[] getDataBaseInfo = new string[size];                    for (int i = 0; i < size; i++)
                    {
                        getDataBaseInfo[i] = xmlOperator.GetXmlReader()[i];
                    }
                    string G_Str_ConnectionString = "Server=" + getDataBaseInfo[0];
                    G_Str_ConnectionString += ";user id=" + getDataBaseInfo[1];
                    G_Str_ConnectionString += ";password=" + getDataBaseInfo[2];
                    G_Str_ConnectionString += ";initial catalog=" + getDataBaseInfo[3] + ";Connect Timeout=20";                    G_Con = new SqlConnection(G_Str_ConnectionString);
                    G_Con.Open();
                }
                else
                {
                    MessageBox.Show("不存在配置文件,请添加!");
                    Application.Exit();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据库打开失败,错误原因:" + ex.Message.ToString());
            }
            return G_Con;
        }        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="cmdtxt">要执行的SQL语句</param>
        /// <returns></returns>
        public bool GetExecute(string cmdtxt)
        {
            try
            {
                G_Com = new SqlCommand(cmdtxt, GetCon());                if (G_Com.ExecuteNonQuery() > 0)
                {
                    int count = G_Com.ExecuteNonQuery();
                    MessageBox.Show(count.ToString());
                    return true;
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("GetExecute函数错误:" + ex.Message.ToString());
                return false;
            }
            finally
            {
                if (GetCon().State == ConnectionState.Open)
                {
                    GetCon().Close();
                    G_Com.Dispose();
                }
            }
        }        #endregion        #region   返回数据
        /// <summary>
        /// 返回数据集类型
        /// </summary>
        /// <param name="cmdtxt">需要查询的SQL语句</param>
        /// <returns></returns>
        public DataSet GetDs(string cmdtxt, string table)
        {
            try
            {
                G_Da = new SqlDataAdapter(cmdtxt, GetCon());
                G_Ds = new DataSet();
                G_Da.Fill(G_Ds, table);
                return G_Ds;
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误:" + ex.Message.ToString());
                return null;
            }
            finally
            {
                if (GetCon().State == ConnectionState.Open)
                {
                    GetCon().Close();
                    this.Dispose();
                }
            }
        }
        /// <summary>
        /// 返回SqlDataReader类型数据
        /// </summary>
        /// <param name="cmdtxt">要执行的SQL语句</param>
        /// <returns></returns>
        public SqlDataReader GetReader(string cmdtxt)
        {
            G_Com = new SqlCommand(cmdtxt, GetCon());  //声明SqlCommand对象
            SqlDataReader P_Dr;
            try
            {
                P_Dr = G_Com.ExecuteReader(CommandBehavior.CloseConnection);
                return P_Dr;
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误:" + ex.Message.ToString());
                return null;
            }
        }
每次调用GetExecute()这个函数
大概程序关闭 然后 启动一两次之后会提示,可是我的代码里面加了finally{}每次用完都有关闭啊 !
现在都不知道为什么会这样,难道我操作太频繁了,还没来得及关闭就又创建了一次????
高手们,帮一下忙。。如果你遇到过这种情况的话
GetExecute函数错误:ExecuteNonQuery要求已打开且可用连接。连接的当前状态为打开
还会提示:GetExecute函数错误:内部连接致命错误!

解决方案 »

  1.   


    GetExecute函数错误:ExecuteNonQuery要求已打开且可用连接。连接的当前状态为打开
    还会提示:GetExecute函数错误:内部连接致命错误!肯定是没关闭呀,要释放连接
      

  2.   

    或者请问该在什么地方释放比较合适?            finally
                {
                    if (GetCon().State == ConnectionState.Open)
                    {
                        GetCon().Close();
                        G_Com.Dispose();
                    }
                }
    上面这一段代码难道没有释放吗?不理解啊 !!!该怎么做才能释放?
      

  3.   

    大概程序关闭 然后 启动一两次之后会提示,可是我的代码里面加了finally{}每次用完都有关闭啊 !
    现在都不知道为什么会这样,难道我操作太频繁了,还没来得及关闭就又创建了一次????可能,那还没释放你又连接上了
      

  4.   

     public bool GetExecute(string cmdtxt)
            {
                try
                {
                    G_Com = new SqlCommand(cmdtxt, GetCon());                if (G_Com.ExecuteNonQuery() > 0)
                    {
                        int count = G_Com.ExecuteNonQuery();
                        MessageBox.Show(count.ToString());
                        return true;
                    }
                    else
                        return false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("GetExecute函数错误:" + ex.Message.ToString());
                    return false;
                }
                finally
                {
                    if (GetCon().State == ConnectionState.Open)
                    {
                        GetCon().Close();
                        G_Com.Dispose();
                    }
                }
            }
    你每call一次 GetCon()返回的就是一个新的连接。
      

  5.   

    请问有没有优良的 数据库操作类 代码?
    我也清楚它确实是每call一次 GetCon()返回的就是一个新的连接
    该如何解决啊!说出解决方法!!
      

  6.   


    就算是每Call一次我每一次到最后不是也GetCon().Close()了吗?
      

  7.   

    清楚了那肯定是没有释放掉呀,第一次CALL一个,第二个次又CALL一个,是不同的,释放了最后那一个?
      

  8.   


    你关掉的是GetCon()返回的连接.前一次的你没有关闭啊。修改后大概是这样子的,试试。public bool GetExecute(string cmdtxt)
            {
                try
                {
                    SqlConnection cn=GetCon();
                    G_Com = new SqlCommand(cmdtxt, cn);                if (G_Com.ExecuteNonQuery() > 0)
                    {
                        int count = G_Com.ExecuteNonQuery();
                        MessageBox.Show(count.ToString());
                        return true;
                    }
                    else
                        return false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("GetExecute函数错误:" + ex.Message.ToString());
                    return false;
                }
                finally
                {
                    if (cn.State == ConnectionState.Open)
                    {
                        cn.Close();
                        G_Com.Dispose();
                    }
                }
            }
      

  9.   

    不明白!  
    SqlConnection cn=GetCon();
    G_Com = new SqlCommand(cmdtxt, cn);

    G_Com = new SqlCommand(cmdtxt, GetCon();
    不是一样的概念吗?
      

  10.   

                              PBS VPS枪文
    之前使用的VPS很不稳定,正好也要到期了,于是我就对了几家的VPS,最终确定是PBS VPS ,套餐选了最廉价的Standard,不过我选到了最想要的机房:HK Mege,这是我目前为止,从HK到国外最快的VPS了。配置:
         硬盘:15G
         内存:256M
         IP  :  1个 HK出口(可以提供电信/网通/国际出口)
         Hot backup : 1
    PBS VPS刚买的时候有点担心,因为一个月180块,如果买便宜的VPS,可以买好多个。在买之前我联系了他们试用了两天,打消了我的疑虑。我主要看中了有HK的出口,还有hot backup的功能,而且那里的NOC团队也给我了很大的帮助。在试用的两天,我装了几次系统和遇到了几个问题,他们的NOC团队总是能给我最好的解决。我让我HK和美国的朋友测试一下到我的网站速度,他们的感觉都是好快的。这点我是比较满意的。
    有点不满意的是他们那里提供的VPS只有CentOS版的,我原本想要RHEL版的,不过他那里没有,不过都是相通的,就用了CentOS了。
    好了,大家也可以去试试PBS VPS,有想了解的,可以站内发短信给我。
      

  11.   

    这个应该正确了,你的GetCon()可以创建了SqlConnection对象并返回了他的引用而你在调用时没有得到他的引用,这样就不能显示去释放连接,只有等到程序关闭了
      

  12.   


    public DataSet GetDs(string cmdtxt, string table)
            {
                try
                {
    SqlConnection conn = GetCon();
                    G_Da = new SqlDataAdapter(cmdtxt, conn);
                    G_Ds = new DataSet();
                    G_Da.Fill(G_Ds, table);
                    return G_Ds;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("错误:" + ex.Message.ToString());
                    return null;
                }
                finally
                {
    //释放资源
                    if (G_Da != null)
                    {
                        G_Da.Dispose();
                    }                if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                        this.Dispose();
                    }
                }
            }