这是我用来从数据库获取数据的功能
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;
}
问题是:
谢谢。
答案 0 :(得分:0)
我希望这对您有帮助
答案1: 如果我没记错的话,SQLConnection将在后台管理连接池。
关于此主题,这里有一篇不错的文章: https://msdn.microsoft.com/en-us/library/ms971481.aspx
此外,最佳做法是使用“ using”关键字来包装您的连接,因为一旦范围退出(无论有无异常),它将自动关闭您的连接
using(SqlConnection conn = new SqlConnection())
{
conn.Open();
//Do Work with connection
} //Connection automatically closes here without the need to call conn.Close()
答案2: Sql Connection open slow down or make application performance better
答案3: What is Best Approach for Opening/Closing SqlConnection in C#