当前位置:编程学习 > C#/ASP.NET >>

asp.net中怎么判断数据库连接未关闭呢?

追问:这个我知道,我就是想知道怎么查看有几个连接。

你还真是多面手啊,从Linux到C++,还会.net   呵呵

答案:调用个close方法就可以了,如果以后要打开的话就不会出错了,如果以后打开的时候出错,那数据库链接就是没有关闭了

数据库连接类 SqlHelper
private static string connectionString="Data Source=.;Initial Catalog=HLRZ;User ID=sa;pwd=21";
       public static SqlConnection connection = new SqlConnection(connectionString);

数据查询类:
public List<HLContent> selecttitleandContent(int content_typeID)
        {
            string sql = "select * from HLContent where content_typeID='" + content_typeID + "'";
            List<HLContent> list = new List<HLContent>();
            try
            {
                SqlCommand comm = new SqlCommand(sql, SqlHelper.connection);
                SqlHelper.connection.Open();
                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    HLContent content = new HLContent();
                    if (dr["Content_content"].ToString().Length > 20)
                    {
                        content.Content_content = dr["Content_content"].ToString().Substring(0, 20);
                    }
                    else
                    {
                        content.Content_content = dr["Content_content"].ToString();
                    }
                    content.Content_name = dr["Content_name"].ToString();
                    content.ContentID = (int)dr["ContentID"];
                    list.Add(content);
             }
                dr.Close();
                return list;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                SqlHelper.connection.Close();
            }
        }

上一个:学ASP.NET有什么快速入门的方法
下一个:ASP.NET如何把视频文件放到一个DataList中

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,