类型“System.DBNull”的对象无法转换为类型“System.String”
行 35:                                {
行 36:                                    if (columns[i].ColumnName == properties[j].Name)
行 37:                                    { properties[j].SetValue(type, currentRow[i], null); }
行 38:                                }
行 39:                            }           public static List<T> ConvertToList<T>(DataTable table) where T : new()
           {
               //置为垃圾对象 
               List<T> list = null;
               if (table != null)
               {
                   DataColumnCollection columns = table.Columns;
                   int columnCount = columns.Count;
                   T type = new T();
                   Type columnType = type.GetType();
                   PropertyInfo[] properties = columnType.GetProperties();
                   if (properties.Length == columnCount)
                   {
                       list = new List<T>();
                       foreach (DataRow currentRow in table.Rows)
                       {
                           for (int i = 0; i < columnCount; i++)
                           {
                               for (int j = 0; j < properties.Length; j++)
                               {
                                   if (columns[i].ColumnName == properties[j].Name)
                                   { properties[j].SetValue(type, currentRow[i], null); }//说是这个行出错,怎么修改这个方法呢
                               }
                           }
                           list.Add(type); type = new T();
                       }
                   }
                   else { list = null; }
               }
               else
               {
                   throw new ArgumentNullException("参数不能为空");
               }
               return list;
           }

解决方案 »

  1.   

    pregkey.SetValue( "BackBitmap ",怎么设置成BINARY);
    就在后面写值实例
      

  2.   

    你在数据库中读取了null值,并试图转换为string类型,建议每读一个字段的时候用SqlDataReader的IsDBNull方法检测一下null值,或给数据库所有字段默认值
      

  3.   

    搞定 结贴public static List<T> ConvertToList<T>(DataTable table) where T : new()
               {
                   //置为垃圾对象 
                   List<T> list = null;
                   if (table != null)
                   {
                       DataColumnCollection columns = table.Columns;
                       int columnCount = columns.Count;
                       T type = new T();
                       Type columnType = type.GetType();
                       PropertyInfo[] properties = columnType.GetProperties();
                       if (properties.Length == columnCount)
                       {
                           list = new List<T>();
                           foreach (DataRow currentRow in table.Rows)
                           {
                               for (int i = 0; i < columnCount; i++)
                               {
                                   for (int j = 0; j < properties.Length; j++)
                                   {
                                       if (columns[i].ColumnName == properties[j].Name)
                                       {
                                           if (currentRow[i]!= DBNull.Value)
                                           {
                                               properties[j].SetValue(type, currentRow[i], null);
                                           }

                                       }
                                   }
                               }
                               list.Add(type); type = new T();
                           }
                       }
                       else { list = null; }
                   }
                   else
                   {
                       throw new ArgumentNullException("参数不能为空");
                   }
                   return list;
               }