Open FreeVB opened 3 years ago
WriteFile()
takes a List
not a DataSet
or a DataTable
I used the following code to convert the datatable to a list, but it prompted an error.
public static IList<T> DataTableToILists<T>(DataTable p_Data) { if (p_Data == null) return null; IList<T> result = new List<T>(); for (int j = 0; j < p_Data.Rows.Count; j++) { T _t = (T)Activator.CreateInstance(typeof(T)); PropertyInfo[] propertys = _t.GetType().GetProperties(); foreach (PropertyInfo pi in propertys) { for (int i = 0; i < p_Data.Columns.Count; i++) { // 属性与字段名称一致的进行赋值 if (pi.Name.Equals(p_Data.Columns[i].ColumnName) && pi.CanWrite) { // 数据库NULL值单独处理 if (p_Data.Rows[j][i] != DBNull.Value) { if (pi.PropertyType.Equals(typeof(string))) { pi.SetValue(_t, p_Data.Rows[j][i].ToString(), null); } else { pi.SetValue(_t, p_Data.Rows[j][i], null); } } else { pi.SetValue(_t, null, null); } break; } } } result.Add(_t); } return result; }
Error message:
The object reference is not set to the instance of the object.
fastCSV.ReadFile The read list can be saved successfully with WriteFile, but DataGridView or datatable cannot be saved. Am I not using it correctly?