我正在使用带有MySQL后端的C#创建客户端 - 服务器应用程序。我试图弄清楚创建了哪些对象乘以用于mysql的连接。 问题的根源是没有正确结束连接。
我正在寻找一些方法来检查Visual Studio内部/外部创建连接的所有对象。
从MySQL方面我看到所有连接,其中有多少来自哪个客户端,但在客户端网站上我不知道哪个对象/后台工作者正在进行连接。
答案 0 :(得分:0)
从我读到的内容我认为您要确保您的应用程序只打开1个连接并确保连接正确关闭。
我正在使用一个类来连接数据库,该数据库只有一个select方法可以打开和关闭连接,这样做的好处就是你不需要单身,或者需要担心多个线程,因为什么时候选择方法执行连接自动关闭:
/// <summary>
/// class which controlls the database,
/// </summary>
public class DBConnect
{
private MySqlConnection connection;
/// <summary>
/// the constructor which starts to initialize the database.
/// </summary>
/// <param name="username"></param>
/// <param name="password"></param>
public DBConnect(string username, string password)
{
Initialize(username, password);
}
/// <summary>
/// initiates connection string for the database.
/// </summary>
/// <param name="username"></param>
/// <param name="password"></param>
private void Initialize(string username, string password)
{
string server = "Localhost";
string database = "";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + username + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
/// <summary>
/// opens the connection to the database
/// </summary>
/// <returns></returns>
private bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
//When handling errors, you can catch your application's response based
//on the error number.
//The two most common error numbers when connecting are as follows:
//1042: Cannot connect to server.
//1045: Invalid user name and/or password.
switch (ex.Number)
{
case 1042:
MessageBox.Show("MySqlException: cannot connect to the server");
break;
//case 1045:
// MessageBox.Show("Invalid username/password, please try again");
// break;
}
MessageBox.Show(ex.Message + "\r\n" +
ex.StackTrace);
return false;
}
catch (InvalidOperationException ioe)
{
Console.WriteLine(ioe);
MessageBox.Show("Er is al een connectie geopend");
return false;
}
}
/// <summary>
/// closes the connection with the database
/// </summary>
/// <returns>true or false depending on the succes of closing the connection</returns>
private bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
/// <summary>
/// generally used to get multpile rows of data from the datbase
/// </summary>
/// <param name="query">SQL statement</param>
/// <returns></returns>
public DataTable Select(MySqlCommand cmd)
{
//Open connection
if (this.OpenConnection() == true)
{
//Create Command
cmd.Connection = connection;
//Read the data and store it in a DataTable
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
try
{
da.Fill(dt);
}
catch (MySqlException e)
{
MessageBox.Show("An exception has occured loading the data: \n" + e.Message);
Console.WriteLine(e.Message);
}
//close Connection
this.CloseConnection();
//return list to be displayed
return dt;
}
else
{
return null;
}
}
}
答案 1 :(得分:0)
我正在使用此代码:
public DataSet MySQL_Select(string query)
{
MySql.Data.MySqlClient.MySqlConnection msqlConnection = null;
msqlConnection = new MySql.Data.MySqlClient.MySqlConnection("server=" + MYSQL_SERVER + ";user id=" + MYSQL_login + ";Password=" + MYSQL_password + ";database=" + MYSQL_SCHEMAS + ";persist security info=False; Allow Zero Datetime=true ");
DataSet DS = new DataSet();
try
{
MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(query, msqlConnection);
mySqlDataAdapter.Fill(DS);
}
catch (Exception er)
{
MessageBox.Show(er.Message);
}
finally
{
msqlConnection.Close();
}
return (DS);
}
我能改进吗?为了确保我总是关闭连接,因为从我所知道的最后声明做得恰到好处。
答案 2 :(得分:0)
史蒂夫的评论中提到,用使用块包装;一旦它超出范围,这将关闭并处理连接对象。
using (IDbConnection conn = new MySql.Data.MySqlClient.MySqlConnection(...))
{
using (IDbCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("select ...", conn))
{
conn.Open();
var reader = cmd.ExecuteReader()';
// process reader, etc
}
}
IDisposable 实施者应该在使用中 IDbConnection 不应该是全局变量,它们应该在范围内构造,使用和处理,或者作为参数传递给方法。