using System;

using System.Data;

namespace Sunlib


public static class DataHelper


// Convert database data object to entity value object

//T: Types to convert

//columnName: Name

//defValue: The default value is

public static T ToValue<T>(DataRow dr, string columnName,T defValue)






return (T)(Convert.ChangeType(dr[columnName], typeof(T)))


// If the obtained field value is empty , Return the default value

return defValue;


// Call function ToValue

public void Update()


// Omit getting Table Data part code

DataRow dr=ds.Tables[0].Rows[0];

// Get the corresponding field value of the data table ,ToValue The default is critical , The default value determines the type of acquisition

string MeterNo=ToValue(dr,"MeterNo","");//string Type default with double quotes

decimal Insideno=ToValue(dr,"Insideno",0m);//decimal The default value can be set to 0m perhaps decimal Type values

DateTime Reading=ToValue<DateTime>(dr,"Reading",DateTime.Now);//DateTime The default value can be set to the current value ; As long as the default value type is set correctly ,<> Can be removed





.net c# More articles on converting database data objects to entity value objects

  1. C# take object Object to entity object

    C# take object Object to entity object . There are two ways . The first method , The code is as follows : /// <summary> /// take object Object to entity object /// </summary> ...

  2. JavaScript Rules for converting objects to original values in

    JavaScript What are the principles for converting an object to its original value ? P52 Object to Boolean object to Boolean conversion is very simple : All objects ( Including numbers and functions ) All converted to true. The same is true for packaging objects :new Boolean(fal ...

  3. Go part 7 Reflection , Reflection type object , Reflection value object

    Reflection Reflection refers to the ability to access and modify the program itself while it is running ,( When the program is compiled , Variables are converted to memory addresses , Variable names are not written to the executable by the compiler , When running a program , The program can't get its own information ) Languages that support reflection can be programmed ...

  4. java take JSON String to entity class object , be based on net.sf.json Realization

    @SuppressWarnings("unchecked") public static <T> T jsonToObject(String jsonString, C ...

  5. hold Javascript Object into the key value of the connector string method summary

    307down votefavorite 93 Do you know a fast and simple way to encode a Javascript Object into a strin ...

  6. springMVC List Object to json List objects

    @ResponseBody @RequestMapping("") public List<POJO> getList(){ return List< ...

  7. DataTable Data into entities

    When we write software in a three-tier architecture , We often encounter the following problems , It's the problem of parameter transfer between the three layers : Suppose we were D Layer query data is DataTable Type of , So we're in B Layers even U When the layer uses this data , To use DataTable By type ...

  8. XStream take java Object to xml when , Underscores in object fields “_”, After the conversion, it became a two-way solution

            In a project the other day , Due to the naming of database fields Two of them : One is called " Market price " One is called " Store prices " For the sake of distinguishing , So they were named as market ...

  9. .NET analysis xml character string , Assign values to entity class objects by reflection , Get entity class data list

    /// <summary> /// analysis xml character string Convert to entity class list data /// </summary> /// <param name="xmlStr&quo ...

Random recommendation

  1. Remote deployment ASP.NET MVC 5 project [ Reprint ]

    Extra discourse : I feel that my machine is slow , The configuration is not good , So recently, I want to deploy the previous project to the server of the laboratory , But often not in the lab , So I wonder if I can deploy it remotely . So today, I specially studied the specific process , Let's share with you . I am a novice ...

  2. Java Exception information handling

    import; import java.text.SimpleDateFormat; import java.util.Date; import org.jun ...

  3. mysql Database performance optimization ( Include SQL, Table structure , Indexes , cache )

    Optimize the target to reduce IO frequency IO It's always the easiest place for a database to bottleneck , This is determined by the responsibility of the database , More than in most database operations 90% All the time is IO Occupied by operation , Reduce IO Times is SQL Optimization needs to be the first priority , When ...

  4. file Type allowed file format setting problem ,“ Select File ” Open slowly

    1,file Type of input The properties of the open selection box are controlled by the following two properties : ①multiple="multiple" : You can select multiple files at a time ②accept="image ...

  5. Good project, bad architecture , Four years coder The ridicule

    More than four years , After graduation in a small private enterprise to do front-end :( initial, Yes oo With a deeper understanding : In imitation of a space , A thorough understanding of the front-end technology architecture ): In this company 4 Six months later, I jumped out and wanted to work alone : I took on a small project ...

  6. database —— mySQL Basic operation

    Learning resources : 0. Study the tutorial :MySQL course (   (MySQL Tutorial)turtorialPoint 1. Learning help manuals and platforms : MySQL Learning platform     English manual chm ...

  7. canvas-8searchLight2.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 【MySQL】 Random access to data , Support large data volume

    stay mysql With a random data function , stay mysql We're going to have rand() function , Many friends will use it directly , If hundreds of data are OK , If it's tens of thousands or millions, you'll find , It's wrong to use it directly . Now I'll introduce some optimization methods for random data collection ...

  9. Python Web crawler requests modular (1)

    introduce Requests The only non GMO Python HTTP library , People can enjoy it safely . Warning : Non professional use of other HTTP Libraries can cause dangerous side effects , Include : Safety defects . Redundancy . Reinvent the wheel . Gnawing at documents ...

  10. Use jekyll + github pages Build a personal blog

    1. newly build project Actually github pages There are two uses , You can see it on the official website . One of them is as an individual / The organization's home page ( Each account can only have one ), The other is as github The project owner of the project ...