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;
}
}