C # SQLite is treated as an int type when using cast (sum (a) as decimal)

Source: Internet
Author: User
Tags sqlite



SQLite uses cast (sum (a) as decimal), and if the A fractional part is 0, the field in the table is considered the System.Int64 type when populated to the table.



When a DataTable goes to an entity class in C #, an error occurs andthe object "type System.Int64" cannot be converted to type "System.Decimal".



The original code for the entity class conversion function is as follows:


1 /// <summary>
 2 /// Fill the object list: Fill the entity class with DataTable
 3 /// </ summary>
 4 public List <T> FillModel (DataTable dt)
 5 {
 6 if (dt == null || dt.Rows.Count == 0)
 7 {
 8 return null;
 9             }
10 try
11 {
12 List <T> modelList = new List <T> ();
13 foreach (DataRow dr in dt.Rows)
14 {
15 // T model = (T) Activator.CreateInstance (typeof (T));
16 T model = new T ();
17 for (int i = 0; i <dr.Table.Columns.Count; i ++)
18 {
19 PropertyInfo propertyInfo = model.GetType (). GetProperty (dr.Table.Columns [i] .ColumnName);
20 if (propertyInfo! = Null && dr [i]! = DBNull.Value)
twenty one                         {
22 propertyInfo.SetValue (model, dr [i], null);
twenty three                         }
twenty four                     }
25
26 modelList.Add (model);
27}
28 return modelList;
29}
30 catch (Exception ex) {throw ex;}
31 finally
32 {
33}
34} 


Solution, the conversion of the time to make a judgment, if not the default type, then create a new object according to the actual type, indirect assignment, the modified code is as follows (29,30 line):


1 /// <summary>
 2 /// Fill the object list: Fill the entity class with DataTable
 3 /// </ summary>
 4 public List <T> FillModel (DataTable dt)
 5 {
 6 if (dt == null || dt.Rows.Count == 0)
 7 {
 8 return null;
 9             }
10 try
11 {
12 List <T> modelList = new List <T> ();
13 foreach (DataRow dr in dt.Rows)
14 {
15 // T model = (T) Activator.CreateInstance (typeof (T));
16 T model = new T ();
17 for (int i = 0; i <dr.Table.Columns.Count; i ++)
18 {
19 PropertyInfo propertyInfo = model.GetType (). GetProperty (dr.Table.Columns [i] .ColumnName);
20 if (propertyInfo! = Null && dr [i]! = DBNull.Value)
twenty one                         {
22 if (propertyInfo.PropertyType.FullName == dr [i] .GetType (). FullName)
twenty three                             {
24 propertyInfo.SetValue (model, dr [i], null);
25}
26 else
27 {
28 // If the data type in the Table is not the same as the data type in the custom Entity class, the one in the NTt class shall prevail
29 object a = Activator.CreateInstance (Type.GetType (propertyInfo.PropertyType.FullName), dr [i]);
30 propertyInfo.SetValue (model, a, null);
31}
32}
33}
34 modelList.Add (model);
35}
36 return modelList;
37}
38 catch (Exception ex) {throw ex;}
39 finally
40 {
41}
42} 





C # SQLite is treated as an int type when using cast (sum (a) as decimal)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.