繁体   English   中英

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

[英]How to store database values in dictionary in C#

当我收到国家/地区名称时,我想在我的数据库中搜索国家/地区名称列表,并使用国家/地区名称获取相关的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;
    }

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

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

你可能有这样的公共字典:

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;
}

像这样修改你的方法:

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;
}

如何在程序首次启动时使静态构造函数填充Country Ids的字典,这样您只需要查询一次数据库。 然后调用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];
}

让我们为此做一个懒惰的负担!

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注入的安全性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM