使用 mysql 调试 c# 代码的正确方法
本文关键字:方法 代码 mysql 调试 使用 | 更新日期: 2023-09-27 18:35:11
我正在使用带有MySQL后端的C#制作客户端 - 服务器应用程序。我试图弄清楚创建了哪些对象来增加用于 mysql 的连接。问题的根源是连接未正确结束。
我正在寻找某种方法来检查Visual Studio内部/外部的所有创建连接的对象。
从MySQL方面,我可以看到所有连接,以及其中有多少来自哪个客户端,但是在客户端站点上,我不知道哪个对象/后台工作者正在执行连接。
正如史蒂夫的评论所暗示的那样,用 using 块包装; 一旦连接对象超出范围,这将关闭并释放它。
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
}
}
可识别的实现者应该在使用中。
IDbConnection不应该是全局变量,它们应该在作用域中构造、使用和处置,或者作为参数传递到方法中。
从
我读到的内容来看,我认为您要确保您的应用程序只打开 1 个连接并确保该连接正确关闭。
我正在使用一个类连接到数据库,该数据库只有一个打开和关闭连接的 select 方法,这样做的美妙之处在于您不需要单例或无需担心多个线程,因为当执行 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;
}
}
}
我正在使用以下代码:
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);
}
我可以改进它吗? 确保我都有密切的联系,因为从我所知道的最终陈述中,这样做是正确的。