【发布时间】:2013-03-04 19:32:51
【问题描述】:
这是我第一次在 ASP.Net 上使用 MySql。
与我经常使用的 MSSql 不同,我注意到使用 MySqlConnection 连接到数据库需要很长时间(我的意思是一两秒),
MySql.Data.MySqlClient.MySqlConnection connection = new MySqlConnection(DBConnectionString);
因此,我想知道如何实现一个连接池,或者任何可以存储一个连接对象 (MySqlConnection) 以在整个应用程序中使用的推荐结构。
是否有这样做的常见做法或任何其他建议?
这是我正在使用的代码 - 也许我在这里做错了什么?
MySql.Data.MySqlClient.MySqlConnection connection = new MySqlConnection(DBConnectionString);
MySqlDataAdapter adapter = new MySqlDataAdapter();
if (connection.State != ConnectionState.Open)
{
try
{
connection.Open();
}
catch (MySqlException ex)
{
throw (ex);
}
}
MySqlCommand cmd = new MySqlCommand("SELECT this FROM that", connection);
DataSet ds = new DataSet();
adapter.SelectCommand = cmd;
adapter.Fill(ds);
cmd.Connection.Close();
【问题讨论】:
-
每次尝试都需要这么长时间吗?
-
是的。这是否意味着它不应该那样?