如何在C#中的字典中存储数据库值

时间:2019-04-01 21:54:10

标签: c# sql database

当我收到一个国家名称时,我想在数据库中搜索一个国家名称列表,并获取与该国家名称相关的ID。我现在有

    public static int GetCountryId(string countryName)
    {
        int countryId = 0;
        if (!string.IsNullOrEmpty(countryName))
        {
            var listOfCountries = GetCountries();
            var match = listOfCountries.FirstOrDefault(item => (item.Name).Contains(countryName));
            if (match != null)
            {
                countryId = match.Id;
            }
        }
        return countryId;
    }

    private static List<Country> GetCountries()
    {
        string query = $"SELECT Id, Name FROM Countries";
        List<Country> cases = Common.GetCollection<Country>(Properties.Settings.Default.DbConnectionString, query);
        return cases;
    }

但是,正如您所看到的,每次我想要获取国家/地区名称列表时,都必须查询数据库。我想要它,以便将此列表存储在字典中,而我只能访问字典。

有人会知道我如何改进我的代码,这样我就不必每次都访问数据库了吗?

4 个答案:

答案 0 :(得分:0)

您可能有这样的公共词典:

public static Dictionary<int, string> countries = new Dictionary<int, string>();

如果之前未填充字典,则该方法填充字典;

private static void GetCountries()
    {
        if(countries.Count == 0)
        {
              string query = $"SELECT Id, Name FROM Countries";
              countries = Common.GetCollection<Country>(Properties.Settings.Default.DbConnectionString, query)
              .ToDictionary(x => x.Id, x=> x.Name);
        }
    }


public static int GetCountryId(string countryName)
{
    return countries.Contains(countryName) CountryIds[countryName] : 0;
}

答案 1 :(得分:0)

像这样修改您的方法:

private static List<Country> countries;

private static List<Country> GetCountries()
{
    if (countries == null || countries.Count == 0)
    {
        string query = $"SELECT Id, Name FROM Countries";
        countries = Common.GetCollection<Country>(Properties.Settings.Default.DbConnectionString, query);
    }

    return countries;
}

答案 2 :(得分:0)

如何在首次启动程序时使用静态构造函数填充国家(地区)ID字典,因此您只需查询数据库一次。那么对GetCountryId的调用只能使用该字典吗?

private static Dictionary<string, int> CountryIds;

public static NameOfYourClass(){
    CountryIds = new Dictionary<string, int>();
    string query = $"SELECT Id, Name FROM Countries";
    List<Country> cases = Common.GetCollection<Country>(Properties.Settings.Default.DbConnectionString, query);
    foreach (country Country in cases)
    {
        CountryIDs.Add(Country.Name, Country.Id);
    }        
}

public static int GetCountryId(string countryName)
{
    if(!CountryIds.Contains(countryName) return 0;
    return CountryIds[countryName];
}

答案 3 :(得分:0)

让我们为此懒起来吧!

private Dictionary<string, Country> _countryNames = null;

public Dictionary<string, Country> CountryNames
{
    get
    {
         if(_countryNames == null)
         {
             _countryNames = new Dictionary<int, Country>();
             foreach(var country in GetCountries())
             {
                 _countryNames.Add(country.Name, country)
             }
         }
         return _countryNames;
    }
}

public static int GetCountryId(string countryName)
{
    Country result;
    CountryNames.TryGetValue(countryName, out result);
    if (result == null) return 0;
    return result.Id;
}

private static IEnumerable<Country> GetCountries()
{
    string query = "SELECT Id, Name FROM Countries";
    return Common.GetCollection<Country>(Properties.Settings.Default.DbConnectionString, query);
}

但是通常最好让数据库来做:在需要的地方运行查询,将过滤器字符串传递给数据库。不幸的是,Common.GetCollection<T>()对我们隐藏了这种能力。 query变量应如下所示:

string query = "SELECT Id, Name FROM Countries WHERE Name = @CountryName";

,但是从这里的问题尚不清楚如何提供@CountryName参数值。您应该要做的是使用字符串替换或内插将值直接包含在查询字符串中。那将是非常糟糕;它创建了一种严重的形式,称为SQL注入。