使用了数据库操作类,但是无法释放资源
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;
}
}
}
}
}