我每2秒執行一次相同的命令。我想下面的代碼創建多個連接:創建單個或多個SQL連接多次執行相同的查詢會更好嗎?
[System.Web.Services.WebMethod]
public static int getActivity()
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString()))
{
connection.Open();
using (var cmd = new SqlCommand("SELECT TOP 1 ValueX FROM TABLE WHERE ID= 2 AND EVENTID = 2 ORDER BY DATE DESC", connection))
{
var x = cmd.ExecuteScalar();
int Result;
if (x != null)
{
Result = int.Parse(x.ToString());
Console.WriteLine("USER ACTIVITY : " + Result);
}
else
{
Result = -999;
}
connection.Close();
return Result;
}
}
}
如果我把這個方法幾個時間是否下面的代碼做多連接或單個連接?
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString()))
有人可以解釋我是否需要修改此代碼還是這是好的嗎?
謝謝。