Reading int values from SqlDataReader

后端 未结 6 1509
遥遥无期
遥遥无期 2021-02-02 08:18

hi can anyone please help me with this fetching from database of int values im having difficulty in fetching int values , it works for varchar but not int can someone help me ou

6条回答
  •  天命终不由人
    2021-02-02 08:53

    based on Sam Holder's answer, you could make an extension method for that

    namespace adonet.extensions
    {
      public static class AdonetExt
      {
        public static int GetInt32(this SqlDataReader reader, string columnName)
        {
          return reader.GetInt32(reader.GetOrdinal(columnName));
        }
      }
    }
    

    and use it like this

    using adonet.extensions;
    
    //...
    
    int farmsize = reader.GetInt32("farmsize");
    

    assuming there is no GetInt32(string) already in SqlDataReader - if there is any, just use some other method name instead

提交回复
热议问题