使用了数据库操作类,但是无法释放资源

qikaa 2011-08-06 03:17:39
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace SRSB.Class
{
public class dataop
{
//private string conn = SRSB.Properties.Settings.Default.conn.ToString();
private string conn = SRSB.Properties.Settings.Default.conn.ToString();
public string Conn
{
get { return conn; }
set { conn = value; }
}

private SqlConnection GetSqlConnection()
{
try
{ return new SqlConnection(conn); }
catch
{
throw new ArgumentNullException( "数据库服务器尚未准备好!" );
}
}
/// 创建一个SqlCommand对象以此来执行存储过程
/// </summary>
/// <param name="sqlConnection">sql链接</param>
/// <param name="procedureName">存储过程名称</param>
/// <param name="parameter">存储过程参数</param>
/// <returns>返回SqlCommand对象</returns>
private SqlCommand CreateCommand(SqlConnection sqlConnection, string procedureName, SqlParameter[] parameter)
{
if ( sqlConnection.State == System.Data.ConnectionState.Closed )
sqlConnection.Open();
SqlCommand command = new SqlCommand(procedureName,sqlConnection);
command.CommandType = CommandType.StoredProcedure;

if ( parameter != null )
{
foreach( SqlParameter param in parameter)
{
command.Parameters.Add( param );
}
}

/// 加入返回参数
command.Parameters.Add( new SqlParameter("ReturnValue",SqlDbType.Int,4,ParameterDirection.ReturnValue,false,0,0,String.Empty,DataRowVersion.Default,null) );

return command ;
}
/// 根据SQL语句生成 SqlCommand 对象
/// </summary>
/// <param name="sqlConnection">SqlConnection 连接</param>
/// <param name="strSql">SQL语句</param>
/// <param name="parameter">构造SQL语句参数</param>
/// <returns>返回一个新的 SqlCommand 对象</returns>
public SqlCommand CreateSqlCommand(SqlConnection sqlConnection, string strSql, SqlParameter[] parameter)
{
if ( sqlConnection.State == System.Data.ConnectionState.Closed )
sqlConnection.Open();
SqlCommand command = new SqlCommand(strSql,sqlConnection);

if ( parameter != null )
{
foreach( SqlParameter param in parameter)
{
command.Parameters.Add( param );
}
}
//sqlConnection.Close();
//sqlConnection.Dispose();

return command ;
}
/// 运行 SQL 语句
/// </summary>
/// <param name="strSql">SQL语句</param>
/// <returns>返回int</returns>
public int RunSqlScalar(string strSql)
{
using ( SqlConnection sqlConnection = GetSqlConnection() )
{
SqlCommand cmd = CreateSqlCommand(sqlConnection,strSql,null);
int tmp = (int)cmd.ExecuteScalar();
sqlConnection.Close();
sqlConnection.Dispose();
return tmp;
}
}
/// 运行 SQL 语句 返回DataSet
/// </summary>
/// <param name="strSql">SQL语句</param>
/// <param name="parameter">构建SQL语句参数</param>
/// <returns>返回DataSet</returns>
public DataSet RunSqlDataSet(string strSql, SqlParameter[] parameter)
{
using ( SqlConnection sqlConnection = GetSqlConnection() )
{
SqlCommand cmd = CreateSqlCommand(sqlConnection, strSql, parameter);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet dataSet = new DataSet();
try
{
da.Fill(dataSet);
sqlConnection.Close();
sqlConnection.Dispose();
da.Dispose();
return dataSet;
}
finally
{
sqlConnection.Dispose();
da.Dispose();
dataSet.Dispose();

}
}
}
/**//// <summary>
/// 说明:ExecSQL用来执行SQL语句。
/// 返回值:操作是否成功(True\False)。
/// 参数:sQueryString SQL字符串
/// </summary>
public Boolean ExecSQL(string sQueryString)
{
using (SqlConnection sqlConnection = GetSqlConnection())
{
SqlCommand dbCommand = CreateSqlCommand(sqlConnection, sQueryString, null);
try
{
dbCommand.ExecuteNonQuery();
sqlConnection.Close();
sqlConnection.Dispose();
return true;

}
catch
{
sqlConnection.Close();
return false;
}
finally
{
sqlConnection.Close();
sqlConnection.Dispose();
}

}
}
/**/
/// <summary>
/// 说明:ExecSQL用来执行SQL语句。
/// 返回值:操作是否成功(True\False)。
/// 参数:sQueryString SQL字符串
/// </summary>
public Boolean ExecSQLcount(string strSql, SqlParameter[] parameter)
{
using (SqlConnection sqlConnection = GetSqlConnection())
{
SqlCommand cmd = CreateSqlCommand(sqlConnection, strSql, parameter);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet dataSet = new DataSet();
da.Fill(dataSet);
if (dataSet.Tables[0].Rows.Count > 0)
{
sqlConnection.Close();
sqlConnection.Dispose();
return true;
}
else
{
sqlConnection.Close();
sqlConnection.Dispose();
return false;
}
}
}
}
}
...全文
83 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qikaa 2011-08-19
  • 打赏
  • 举报
回复
dispose以后,仍然没有用!
iihero 2011-08-06
  • 打赏
  • 举报
回复
RunSqlDataSet, 里头,catch一下exception啊。代码肯定有问题。

dbCommand为什么不dispose,在connection释放之前?
qikaa 2011-08-06
  • 打赏
  • 举报
回复
使用
dataop dataop = new dataop();
string sqlstr = "select * from xwlist where btdm='1'";
DataSet ds = dataop.RunSqlDataSet(sqlstr, null)
*****
****

****
ds.Dispose();

监控了一下,页面打开一次,就增加一个连接,并且无法释放。造成资源耗尽。

2,209

社区成员

发帖
与我相关
我的任务
社区描述
其他数据库开发 其他数据库
社区管理员
  • 其他数据库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧