c#
  • arrays
  • arraylist
  • 2014-02-12 74 views 0 likes 
    0

    我嘗試將字符串[]添加到列表添加陣列列出具體的關鍵

    代碼:

    foreach (int software_id in softwarelist) 
    { 
        String sqlstring = "SELECT SoftwareName, Task_idn_Install, Task_idn_Deinstall, KZ, ParameterFeld FROM TUI_Software WHERE Sw_idn = '" + software_id + "'"; 
        SqlCommand cmd1 = new SqlCommand(sqlstring, con); 
        SqlDataReader result_sw_info = cmd1.ExecuteReader(); 
    
        if (result_sw_info.HasRows) 
        { 
         result_sw_info.Read(); 
    
         softwarename = result_sw_info[0].ToString(); 
         task_idn_install = result_sw_info[1].ToString(); 
         task_idn_deinstall = result_sw_info[2].ToString(); 
         indicator = result_sw_info[3].ToString(); 
         parameter = result_sw_info[4].ToString(); 
    
         string[] software_data = new string[] {sw_id, softwarename, task_idn_install, task_idn_deinstall, indicator, parameter }; 
    
         softwarelist_insert.Add(software_data); 
        } 
        result_sw_info.Close(); 
    } 
    

    所以在softwarelist_insert鍵從0開始。但我怎麼可以添加軟件數據與foreach中的關鍵software_id。因爲我想用software_id標識列表中的arrys。

    +1

    當心你[SQL注入漏洞](http://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work) –

    +0

    是啊,這只是一個例子我找到。用於原始代碼pdo。 – mnlfischer

    +0

    你需要將它放在一個數組中嗎?還是可以創建一個對象來保存軟件信息並擁有該列表 – owen79

    回答

    2

    使用System.Collections.Generic Dictionary<string, string[]>而不是List<string[]>

    var softwarelist_insert = new Dictionary<string, string[]>(); 
    ... 
    softwarelist_insert.Add(software_id, software_data); 
    

    更好的是,使用強類型對象而不是字符串[]。

    public class SoftwareData { 
        public string SoftwareName { get; set; } 
        public string TaskIdnInstall { get; set; } 
        ... 
    } 
    

    然後

    var softwarelist_insert = new Dictionary<string, SoftwareData>(); 
    ... 
    var = new SoftwareData { 
        SoftwareName = result_sw_info[0].ToString(), 
        TaskIdnInstall = result_sw_info[1].ToString(), 
    } 
    softwarelist_insert.Add(software_id, data); 
    
    0

    您可以創建自己的類型和添加數據。 首先創建這樣

    public class softwarelist_insert_class 
    { 
        public string ID {get;set;} 
        public List<string>softwarelist_insert{get;set;} 
    } 
    

    類然後你就可以創建這種類型

    List<softwarelist_insert_class> variable_name=new List<softwarelist_insert_class>(); 
    

    然後你就可以添加數據的列表變量,如:

    List<string> software_data = List<string> {sw_id, softwarename, task_idn_install, task_idn_deinstall, indicator, parameter }; 
    
    variable_name.Add({id,software_data}); 
    
    相關問題