How to extend the value of the SQL statement

Source: Internet
Author: User

To be surprised, we can reduce the numberCodeIt saves us time, but makes some small extensions for convenience. Some content comes from the network.

Insert dB. Find <t> (t) using the object class ):

Public iqueryable <tentity> Find <tentity> (tentity OBJ) Where tentity: Class
{
// Obtain information about all properties
Propertyinfo [] properties = obj. GetType (). getproperties (bindingflags. Public | bindingflags. instance );
// Construct the initial query

Iqueryable <tentity> query = This. gettable <tentity> (). asqueryable <tentity> ();
// Traverse each property
Foreach (propertyinfo P in properties)
{
If (P! = NULL)
{
Type T = P. propertytype;
// Add object, binary, and xdocument to support ing of SQL _variant, imager, and XML.
If (T. isvaluetype | T = typeof (string) | T = typeof (system. byte [])
| T = typeof (object) | T = typeof (system. xml. LINQ. xdocument)
| T = typeof (system. Data. LINQ. Binary ))
{
// If it is not null, it is considered as a condition.

If (P. getvalue (OBJ, null )! = NULL)
{
If (columnattribute) (p. getcustomattributes (typeof (columnattrites), true) [0]). isprimarykey & convert. toint32 (P. getvalue (OBJ, null) = 0)
{

}
Else
{

// Construct a LINQ expression
Parameterexpression Param = expression. parameter (typeof (tentity), "C ");
Expression right = expression. Constant (P. getvalue (OBJ, null ));
Expression left = expression. Property (Param, P. Name );
Expression Filter = expression. Equal (left, right );
Expression <func <tentity, bool> Pred = expression. Lambda <func <tentity, bool> (filter, Param );
Query = query. Where (Pred );
}
}
}
}
}
Return query;
}

Query through the primary key of the class:

Public tentity findkey <tentity> (object Value) Where tentity: Class
{
// Obtain information about all properties
Propertyinfo [] properties = typeof (tentity). getproperties (bindingflags. Public | bindingflags. instance );
// Construct the initial query
Iqueryable <tentity> query = This. gettable <tentity> (). asqueryable <tentity> ();
// Traverse each property
Foreach (propertyinfo P in properties)
{
If (P! = NULL)
{
Type T = P. propertytype;
// Add object, binary, and xdocument to support ing of SQL _variant, imager, and XML.
If (T. isvaluetype | T = typeof (string) | T = typeof (system. byte [])
| T = typeof (object) | T = typeof (system. xml. LINQ. xdocument)
| T = typeof (system. Data. LINQ. Binary ))
{
// If it is not null, it is considered as a condition.

If (columnattribute) (p. getcustomattributes (typeof (columnattribute), true) [0]). isprimarykey)
{
Parameterexpression Param = expression. parameter (typeof (tentity), "D ");
Expression right = expression. Constant (value );
Expression left = expression. Property (Param, P. Name );
Expression Filter = expression. Equal (left, right );

Expression <func <tentity, bool> Pred = expression. Lambda <func <tentity, bool> (filter, Param );

Query = query. Where (Pred );
Break;

}

 

}
}
}
Return query. First ();
}

 

}

Update through object classes:

 

Public void update <tentity> (tentity OBJ) Where tentity: Class
{
String STR = "Update [" + typeof (tentity). Name + "] Set ";
String Cols = "";
String where = "";
// Obtain information about all properties
Propertyinfo [] properties = obj. GetType (). getproperties (bindingflags. Public | bindingflags. instance );
// Construct the initial query

Iqueryable <tentity> query = This. gettable <tentity> (). asqueryable <tentity> ();
// Traverse each property
Foreach (propertyinfo P in properties)
{
If (P! = NULL)
{
Type T = P. propertytype;
// Add object, binary, and xdocument to support ing of SQL _variant, imager, and XML.
If (T. isvaluetype | T = typeof (string) | T = typeof (system. byte [])
| T = typeof (object) | T = typeof (system. xml. LINQ. xdocument)
| T = typeof (system. Data. LINQ. Binary ))
{
// If it is not null, it is considered as a condition.

If (P. getvalue (OBJ, null )! = NULL)
{
If (columnattribute) (p. getcustomattributes (typeof (columnattribute), true) [0]). isprimarykey)
{
Where + = "Where [" + P. Name + "] =" + P. getvalue (OBJ, null );
}
Else
{

If (! (T. tostring (). tolower (). Contains ("string") | T. tostring (). tolower (). Contains ("datetime ")))
Cols + = "[" + P. Name + "] =" + P. getvalue (OBJ, null) + ",";
Else
Cols + = "[" + P. Name + "] = '" + P. getvalue (OBJ, null) + "',";
}
}
}
}
}

STR + = cols. substring (0, cols. Length-1) + where;
Httpcontext. Current. response. Write ("<br>" + STR + "<br> ");
This. executecommand (STR );

}
Public void updateall <tentity> (ienumerable <tentity> OBJ) Where tentity: Class
{
Foreach (VAR item in OBJ)
{
This. Update <tentity> (item );
}
}

Similar to updating, deleting, and adding code, you can also perform data operations on multiple data entries. The code is similar to the update. The db. CS file in the Code http://fccms.googlecode.com/files/linqtoaccess.rar.

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.