@RemusRusanuはよく説明しました。SQLコマンドのキーワードは使用できません。TOP
ただし、一部のTOP
データを選択する場合は、以下の代替ソリューションを使用できます。
データベースからすべてのレコードを選択していますが、リストにレコードを10個だけ入れて、このリストをダッシュボードに戻しています。だから、最新の10枚のレコードを受け取るたびに。
TOP
キーワードなしで現在のSQLコマンドを使用できます。私のは:
SQLコマンド:
Select [Id], [Name] FROM dbo.CUSTOMER where InsertDate = @InsertDate ORDER BY [ID] DESC;
次に、アプリケーションで、トップカウントに基づいてリストに入力できます。
以下のソースコードに関する私のコメントを確認してください。
public List<CustomerModel> GetAllCustomer()
{
List<CustomerModel> lstCustomerModel = new List<CustomerModel>();
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
string InsertDate = string.Empty;
InsertDate = DateTime.Now.ToString("yyyyMMdd");
string query = "SELECT [Id] " +
",[Name] " +
"FROM [dbo].[Customer] where InsertDate = " + InsertDate + " ORDER BY [Id] DESC;";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.CommandType = CommandType.Text;
cmd.Notification = null;
SqlDependency.Stop(connectionString);
SqlDependency.Start(connectionString);
SqlDependency sqlDependency = new SqlDependency(cmd);
sqlDependency.OnChange += OnDependencyChange;
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
int counter = 0;
while (reader.Read())
{
if (counter == 10) /* Here, I am reading just first ten record. After 10 records, I am not filling my List. So, It is kind of top 10 records.. Alternative solution.*/
break;
counter++;
lstCustomerModel.Add(new CustomerModel
{
Id = Convert.ToInt32(reader.GetValue("Id")),
Name = WeightUnit = reader.GetValue("Name")
});
//break;
}
}
}
}
return lstCustomerModel;
}
private void OnDependencyChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change)
{
_context.Clients.All.SendAsync("refreshCustomers");
}
}