【问题标题】:structure like array类似数组的结构
【发布时间】:2016-06-21 16:04:58
【问题描述】:

我正在从我的数据库中选择 2 列 - PPIDNAME。我想存储这两个相关的值。

假设现在我们有了这个:

|PPID |NAME
|1    |Admin
|2    |Aleksa
|3    |Marija
|4    |Predrag
|5    |Bojana

现在在组合框中,我显示Names,当有人按下例如Predrag 时,我想要他的ppid (4) 以供进一步使用。

我尝试了结构,但效果不佳。如果你有什么想法给我写信。请记住,这个例子有 5 行,但实际上我不知道有多少行,所以它不能是静态的。

【问题讨论】:

  • Dictionary<String, int>?如果名称是唯一的
  • 名称不是唯一的,ppid 是唯一的,但据我所知,我可以使用相同的名称,但 ?

标签: c# mysql sql firebird


【解决方案1】:

尝试使用Dictionary 并将您的组合框绑定到字典。

例如(假设 PPID 是唯一的):

Dictionary<int, string> myDictionary = new Dictionary<int, string>
            {
                {1, "Admin"},
                {2, "Aleksa"},
                {3, "Marija"},
                {4, "Predrag"},
                {5, "Bojana"}
            };

【讨论】:

    【解决方案2】:

    编写一个类并让数据库中的每个人都有自己的实例怎么样?

    class YourClass
    
    { 
    public integer PPID {get;set;}
    public string Name {get;set;}
    }
    
    static void Main()
    {
    // Just an example...
    // SQL command already executed, data readers declared and so on...
    
    int i=0;
    
    YourClass[] arr = new YourClass[];
    
    while(yourDataReader.Read()){
      arr[i] = new YourClass();
      arr[i].PPID = yourDataReader.GetInt32(0);
      arr[i].Name = yourDataReader.GetString(1);
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-03
      • 1970-01-01
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 2018-04-26
      相关资源
      最近更新 更多