Use the extension method to extend the function of the LINQ to SQL classes so that the system returns results when adding, deleting, modifying, and deleting data.

Source: Internet
Author: User

I created a new LINQ to SQL classes item named etc. dbml. Etcdatacontext is generated by default.

The system returns a null value when executing etcdatacontext. submitchanges (), so that the user cannot know whether the execution result is successful or failed. Therefore, we decided to use the extension method to expand etcdatacontext.

CodeAs shown in the following code:DataaccesscomponentClass, which is mainly used for configurationConnectionstring. All otherDatacontextThe operation classes inherit from this class and use this class to instantiate datacontext. StaticExtensionsClass is my extension method for etcdatacontext.

Note that the extension method must be a static class, the input parameter is an extension object, and the this keyword is added before the parameter.

Public   Class Dataaccesscomponent
{
///   <Summary>
/// Get connection string of Databse
///   </Summary>
Protected Etcdatacontext
{
Get
{
Return   New Etcdatacontext (configurationmanager. connectionstrings [ " Etcconnectionstring " ]. Connectionstring );
}
}
}

///   <Summary>
/// Extended method class
///   </Summary>
Public   Static   Class Extensions
{
///   <Summary>
/// Update and return results
///   </Summary>
///   <Param name = "etcdatacontext"> </param>
///   <Returns> True: true; false: Error </Returns>
Public   Static   Bool Submitchangeswithresult ( This Etcdatacontext)
{
Bool Result =   False ;
Try
{
Etcdatacontext. submitchanges ();
Result =   True ;
}
Catch (Exception ex)
{
Result =   False ;
}
Return
Result;
}

}

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.