mysql数据库连接未释放怎么解决

chenlilipc 2012-10-25 10:18:18
sqlhelper有自动关闭的,但是在mysql中怎么监视

比如说总共有五个连接,如果连接数大于5就是未释放的
但是这个统计连接数 我怎么看呢

using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using System.Web;
using System.Configuration;

namespace csBll
{
public class MySqlConn
{
public MySqlConn()
{

}
private string database = string.Empty;
public MySqlConn(string databaseName)
{
this.database = databaseName;
}

public MySqlConnection CreateConn()
{
string connStr = HttpContext.Current.Server.MapPath(ConfigurationSettings.AppSettings["mysqlStr"]);
string[] connStrs = connStr.Split('\\');
string connString = connStrs[connStrs.Length - 1].Replace("meetme",database);
MySqlConnection myConn = new MySqlConnection(connString);
return myConn;
}

public bool ExecSQL(string sqlStr)
{
MySqlConnection myConn = CreateConn();
myConn.Open();
MySqlCommand cmd = myConn.CreateCommand();
cmd.CommandText = sqlStr;
if (cmd.ExecuteNonQuery() > 0)
{
myConn.Close();
return true;
}
else
{
myConn.Close();
return false;
}
}

public MySqlDataReader ReturnDataSet(string sqlStr)
{
MySqlConnection myConn = CreateConn();
myConn.Open();
MySqlCommand cmd = myConn.CreateCommand();
cmd.CommandText = sqlStr;

MySqlDataReader rd = cmd.ExecuteReader();

//myConn.Close()-------这个注释了影响吗
return rd;
}

public System.Data.DataSet ExecuteDataSet(string p)
{
throw new Exception("The method or operation is not implemented.");
}
}
}
...全文
1303 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
XBodhi. 2012-10-25
  • 打赏
  • 举报
回复
发邮箱,我给你个类吧,代码多没有贴完。 我自己写的,或是你可以用 微软的 SqlHeler.cs 改下,
chenlilipc 2012-10-25
  • 打赏
  • 举报
回复
这两段代码我只是大概的知道了一个框架

下面那个dispose是重新写个类 那上面的那段代码是在我原有的地方加吗

不知道怎么用,才接触这个一头雾水
XBodhi. 2012-10-25
  • 打赏
  • 举报
回复
public void Dispose()
{
if (this.DBConnection.State != ConnectionState.Closed)
{
this.DBConnection.Dispose();
}
GC.Collect();
}

internal int ExecuteNonQuery(string commandText)
{
int num;
try
{
num = this.CreateCommand(commandText).ExecuteNonQuery();
}
catch (MySqlException exception)
{
throw new CustomException(exception.Message, exception);
}
return num;
}

internal int ExecuteNonQuery(string commandText, MySqlParameter[] parameters)
{
int num;
try
{
num = this.CreateCommand(commandText, parameters).ExecuteNonQuery();
}
catch (MySqlException exception)
{
throw new CustomException(exception.Message, exception);
}
return num;
}

internal int ExecuteNonQueryProcedure(string commandText)
{
int num;
try
{
num = this.CreateCommand(commandText, CommandType.StoredProcedure).ExecuteNonQuery();
}
catch (MySqlException exception)
{
throw new CustomException(exception.Message, exception);
}
return num;
}

internal int ExecuteNonQueryProcedure(string commandText, MySqlParameter[] parameters)
{
int num;
try
{
num = this.CreateCommand(commandText, CommandType.StoredProcedure, parameters).ExecuteNonQuery();
}
catch (MySqlException exception)
{
throw new CustomException(exception.Message, exception);
}
return num;
}

internal int ExecuteNonQueryProcedureTransaction(string commandText)
{
int num;
try
{
num = this.CreateCommandTransaction(commandText, CommandType.StoredProcedure).ExecuteNonQuery();
}
catch (MySqlException exception)
{
throw new CustomException(exception.Message, exception);
}
return num;
}

internal int ExecuteNonQueryProcedureTransaction(string commandText, MySqlParameter[] parameters)
{
int num;
try
{
num = this.CreateCommandTransaction(commandText, CommandType.StoredProcedure, parameters).ExecuteNonQuery();
}
catch (MySqlException exception)
{
throw new CustomException(exception.Message, exception);
}
return num;
}

internal int ExecuteNonQueryTransaction(string commandText)
{
int num;
try
{
num = this.CreateCommandTransaction(commandText).ExecuteNonQuery();
}
catch (MySqlException exception)
{
throw new CustomException(exception.Message, exception);
}
return num;
}

internal int ExecuteNonQueryTransaction(string commandText, MySqlParameter[] parameters)
{
int num;
try
{
num = this.CreateCommandTransaction(commandText, parameters).ExecuteNonQuery();
}
catch (MySqlException exception)
{
throw new CustomException(exception.Message, exception);
}
return num;
}

XBodhi. 2012-10-25
  • 打赏
  • 举报
回复
这样吧 ,我直接给你个 代码, 正好我前些日期写的, MYSQL ADO.NET 访问类,有不合理的地方 你给我留言。

代码如下:



using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using ESWS;

internal class MySqlHelper : IDisposable
{
private MySqlCommand mySqlCommand;
private MySqlConnection mySqlConnection;
private MySqlDataAdapter mySqlDataAdapter;
private MySqlTransaction myTransaction;

protected virtual void AddParameters(MySqlCommand command, MySqlParameter[] parameters)
{
foreach (MySqlParameter parameter in parameters)
{
command.Parameters.AddWithValue(parameter.ParameterName, parameter.Value);
}
}

internal void BegionTransaction()
{
if (this.DBConnection.State != ConnectionState.Open)
{
this.DBConnection.Open();
}
this.myTransaction = this.DBConnection.BeginTransaction();
}

internal void Commit()
{
this.DBTransaction.Commit();
}

protected MySqlCommand CreateCommand(string commandText)
{
return this.CreateCommand(commandText, CommandType.Text);
}

protected MySqlCommand CreateCommand(string commandText, CommandType commandType)
{
return this.CreateCommand(commandText, commandType, null);
}

protected MySqlCommand CreateCommand(string commandText, MySqlParameter[] parameters)
{
return this.CreateCommand(commandText, CommandType.Text, parameters);
}

protected MySqlCommand CreateCommand(string commandText, CommandType commandType, MySqlParameter[] parameters)
{
return this.CreateCommand(this.DBConnection, commandType, commandText, 30, parameters);
}

protected MySqlCommand CreateCommand(MySqlConnection connection, CommandType commandType, string commandText, int commandTimeout, MySqlParameter[] parameters)
{
MySqlCommand mySqlCommand;
try
{
using (this.mySqlCommand = new MySqlCommand())
{
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
this.mySqlCommand.Connection = connection;
this.mySqlCommand.CommandType = commandType;
this.mySqlCommand.CommandText = commandText;
if ((parameters != null) && (parameters.Length > 0))
{
this.AddParameters(this.mySqlCommand, parameters);
}
this.mySqlCommand.CommandTimeout = commandTimeout;
mySqlCommand = this.mySqlCommand;
}
}
catch (MySqlException exception)
{
throw new CustomException(exception.Message, exception);
}
return mySqlCommand;
}

protected MySqlCommand CreateCommand(MySqlTransaction transaction, CommandType commandType, string commandText, int commandTimeout, MySqlParameter[] parameters)
{
MySqlCommand mySqlCommand;
try
{
using (this.mySqlCommand = new MySqlCommand())
{
if (transaction.Connection.State != ConnectionState.Open)
{
transaction.Connection.Open();
}
this.mySqlCommand.Connection = transaction.Connection;
this.mySqlCommand.CommandType = commandType;
this.mySqlCommand.CommandText = commandText;
if ((parameters != null) && (parameters.Length > 0))
{
this.AddParameters(this.mySqlCommand, parameters);
}
this.mySqlCommand.CommandTimeout = commandTimeout;
mySqlCommand = this.mySqlCommand;
}
}
catch (MySqlException exception)
{
throw new CustomException(exception.Message, exception);
}
return mySqlCommand;
}

protected MySqlCommand CreateCommandTransaction(string commandText)
{
return this.CreateCommandTransaction(commandText, CommandType.Text);
}

protected MySqlCommand CreateCommandTransaction(string commandText, MySqlParameter[] parameters)
{
return this.CreateCommandTransaction(commandText, CommandType.Text, parameters);
}

protected MySqlCommand CreateCommandTransaction(string commandText, CommandType commandType)
{
return this.CreateCommandTransaction(commandText, commandType, null);
}

protected MySqlCommand CreateCommandTransaction(string commandText, CommandType commandType, MySqlParameter[] parameters)
{
return this.CreateCommand(this.DBTransaction, commandType, commandText, 30, parameters);
}

XBodhi. 2012-10-25
  • 打赏
  • 举报
回复
释放连接。

C# 调用 MYSQL 会出现连接池溢出,

1.你可以用 Using(){} 对 有继承 IDisposable 接口的对象进行处理。
2.你直接继承 IDisposable 接口 实现这个接口。
chenlilipc 2012-10-25
  • 打赏
  • 举报
回复
604509023@qq.com

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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