SQL Command Result to Dictionary C# .NET 2.0

后端 未结 5 638
感动是毒
感动是毒 2021-02-01 06:55

I have a simple SQL query (using SqlCommand, SqlTransaction) in .NET 2.0 that returns a table of integer-string pairs (ID, Name). I want to get this data into a dictionary like

5条回答
  •  情歌与酒
    2021-02-01 07:44

    You can do like this.

    // Define your Dictionary 
    IDictionary dictionary = new Dictionary();
    
    // Read your dataTable 
    foreach(DataRow row in dataTable.Rows) {      
           // Add Id and Name respectively
           dictionary.Add(int.parse(row[0].ToString()),row[1].ToString())           
    }
    

提交回复
热议问题