Insert (update) object data through generics

Source: Internet
Author: User

/// <Summary> /// insert data through generics /// </summary> /// <typeparam name = "T"> class name </typeparam> // /<param name = "obj"> class object, to Insert a NULL value, use @ NULL </param> // <returns> The New Record ID inserted </returns> public static int Insert <T> (T obj) {StringBuilder strSQL = new StringBuilder (); strSQL = GetInsertSQL (obj); // Insert the object into the database result = SQLPlus. executeScalar (CommandType. text, strSQL, null); return Convert. isDBNull (result )? 0: Convert. toInt32 (result );} /// <summary> /// update data through generics /// </summary> /// <typeparam name = "T"> class name </typeparam> // /<param name = "obj"> class object, to update a NULL value, use @ NULL </param> // <returns> to update the result, if the value is greater than 0, the Update is successful. </returns> public static int Update <T> (T obj) {StringBuilder strSQL = new StringBuilder (); strSQL = GetUpdateSQL (obj); if (String. isNullOrEmpty (strSQL. toString () {return 0;} // update to the database object result = SQLPlus. exec UteNonQuery (CommandType. Text, strSQL, null); int returnValue = Convert. IsDBNull (result )? 0: Convert. toInt32 (result); return returnValue ;} /// <summary> /// obtain the insert statement of the object /// </summary> /// <typeparam name = "T"> generic </typeparam> // /<param name = "obj"> Object </param> // <returns> returns the insert statement </returns> public static StringBuilder GetInsertSQL <T> (T obj) {string tableKey = GetPropertyValue (obj, BaseSet. primaryKey); string keyValue = GetPropertyValue (obj, tableKey); string tableName = GetPropertyValue (obj, Bas ESet. tableName); Type t = obj. getType (); // obtain the Type StringBuilder strSQL of this class = new StringBuilder (); strSQL. append ("insert into" + tableName + "("); string fields = ""; string values = ""; // use Type again. getProperties get PropertyInfo [] foreach (PropertyInfo pi in t. getProperties () {object name = pi. name; // use pi. getValue to get the value // Replace the SQL injection string value1 = Convert. toString (pi. getValue (obj, null )). replace ("'","''"); // String dataType = pi. PropertyType. ToString (). ToLower (); string properName = name. ToString (). ToLower (); if (! String. IsNullOrEmpty (value1) & properName! = TableKey. ToLower () & properName! = BaseSet. PrimaryKey. ToLower () & properName! = BaseSet. TableName. ToLower () & value1! = BaseSet. DateTimeLongNull & value1! = BaseSet. dateTimeShortNull) {// determines whether it is null if (value1 = BaseSet. NULL) {value1 = "";} fields + = Convert. toString (name) + ","; values + = "'" + value1 + "'," ;}// remove the last one, fields = fields. trimEnd (','); values = values. trimEnd (','); // concatenate an SQL string strSQL. append (fields); strSQL. append (") values ("); strSQL. append (values); strSQL. append (")"); strSQL. append ("; SELECT @ IDENTITY;"); return strSQL;} // <summa Ry> /// obtain the updated SQL string of an object /// </summary> /// <typeparam name = "T"> generic </typeparam> // <param name = "obj"> Object </param> // <returns> returns the insert statement </returns> private static StringBuilder GetUpdateSQL <T> (T obj) {string tableKey = GetPropertyValue (obj, BaseSet. primaryKey); string keyValue = GetPropertyValue (obj, tableKey); string tableName = GetPropertyValue (obj, BaseSet. tableName); StringBuilder strSQL = new StringBuilder (); If (string. isNullOrEmpty (keyValue) {return strSQL;} Type t = obj. getType (); // obtain the Type strSQL of this class. append ("update" + tableName + "set"); string subSQL = ""; string condition = "where" + tableKey + "= '" + keyValue. replace ("'", "'' ") +"' "; // use Type again. getProperties get PropertyInfo [] foreach (PropertyInfo pi in t. getProperties () {object name = pi. name; // use pi. getValue to get the value // Replace the SQL injection string value1 = Convert. toString (pi. getValue (obj, null )). replace ("'", "'' "); // string dataType = pi. propertyType. toString (). toLower (); string properName = name. toString (). toLower (); if (! String. IsNullOrEmpty (value1) & properName! = TableKey. ToLower () & properName! = BaseSet. PrimaryKey. ToLower () & properName! = BaseSet. TableName. ToLower () & value1! = BaseSet. DateTimeLongNull & value1! = BaseSet. dateTimeShortNull) {// determines whether it is null if (value1 = BaseSet. NULL) {value1 = "";} subSQL + = Convert. toString (name) + "= '" + value1 + "'," ;}}// remove the last one. subSQL = subSQL. trimEnd (','); // concatenate the update clause strSQL. append (subSQL); // Add the update condition strSQL. append (condition); return strSQL;} public class BaseSet {public static string NULL {get {return "@ null ";}} public static string DateTimeShortNull {get {return "0001-1-1 0:00:00" ;}} public static string DateTimeLongNull {get {return "0001-01-01 00:00:00 ";}} public static string PrimaryKey {get {return "PrimaryKey" ;}} public static string TableName {get {return "TableName ";}}} # region object example [Serializable] public class SortsInfo {private int _ SortID; private string _ SortName; public string TableName {get {return "Sorts ";}} public string PrimaryKey {get {return "SortID" ;}} public int SortID {get {return _ SortID ;}set {_ SortID = value ;}} public string SortName {get {return _ SortName;} set {_ SortName = value ;}}# endregion

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.