[英]C# DataTable and SqlDataAdapter open, close connection and performance issue
This is a function which I used to get data from database 这是我用来从数据库获取数据的功能
public static DataTable getDataTable(string sql, string tableName)
{
DataTable dt = new DataTable();
try
{
SqlDataAdapter da = new SqlDataAdapter(sql, new SqlConnection(strConn));
da.Fill(dt);
dt.TableName = tableName;
}
catch (Exception)
{
dt = null;
}
return dt;
}
The questions are: 问题是:
Thanks in advance. 提前致谢。
i hope this help you 希望对您有帮助
Answer 1 : If I remember correctly the SQLConnection manages connection pooling behind the scenes. 答案1:如果我没记错的话,SQLConnection将在后台管理连接池。
Here is a nice article about this very subject: https://msdn.microsoft.com/en-us/library/ms971481.aspx 这是有关此主题的不错的文章: https : //msdn.microsoft.com/zh-cn/library/ms971481.aspx
Also, a best practice is to use the "using" keyword to wrap your connection because it will automatically close your connection once the scope exits (with or without an exception) 另外,最佳做法是使用“ using”关键字来包装您的连接,因为一旦作用域退出(无论有无异常),它将自动关闭您的连接。
using(SqlConnection conn = new SqlConnection())
{
conn.Open();
//Do Work with connection
} //Connection automatically closes here without the need to call conn.Close()
Answer 2: Sql Connection open slow down or make application performance better 答案2: Sql Connection打开速度变慢或使应用程序性能更好
Answer 3 : What is Best Approach for Opening/Closing SqlConnection in C# 答案3: 在C#中打开/关闭SqlConnection的最佳方法是什么
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.