簡體   English   中英

在SQL Server CE Winforms中查找數據的最快方法是什么

[英]What is fastest way to find data in SQL Server CE Winforms

我將軟件數據庫從MS Access更改為SQL Server CE。 我無法使用其他數據庫,因為我沒有安裝客戶端的(管理員)權限。

我已經根據需要索引了幾列。 我很困惑在SQL Server CE中查找數據,因為這樣做有幾種方法。 我可以使用SqlCeResultSetSqlCeDataReader和其他方法來查找數據。

因為SqlCeResultSet提供了幾個ResultSetOptions所以使用了哪個,何時使用。 我的ID是表Users主鍵,通常使用3種類型的查詢:

選擇具有where條件的幾列:

SELECT Id, email, mobile
FROM Users
WHERE Users.Tmpid = 3
  AND connected = 1;

用主鍵選擇幾個字段:

SELECT email, mobile, address
FROM Users
WHERE Id = 10;

沒有條件:

SELECT Max(TmpId)
FROM Users;

我很困惑何時使用哪個。 哪一種提供了在SQL Server CE中查找數據的最快方法?

在我的非正式評估中,應該使用一個索引,然后使用TableDirect API(如下面的示例代碼中所示),它的速度要快30%。

public CacheElement FindElementByKey(Guid key)
{
  using (var command = _connection.CreateCommand())
  {
    command.CommandType = CommandType.TableDirect;
    command.CommandText = "CacheElement";
    command.IndexName = "PK_CacheElement";

    using (var reader = command.ExecuteReader())
    {
        reader.Seek(DbSeekOptions.FirstEqual, key);
        if (reader.Read())
        {
            var element = new CacheElement();
            element.Key = key;
            element.Tag = reader.GetValue(1) == DBNull.Value ? null : reader.GetString(1);
            element.Value = (byte[])reader.GetValue(2);
            element.CreatedAt = reader.GetDateTime(3);
            element.ExpirationAt = reader.GetDateTime(4);
            return element;
        }
    }
    return null;
  }
}

請參閱我的博客文章以獲取示例和更多信息: http : //erikej.blogspot.dk/2015/07/sql-server-compact-adonet-data-access.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM