SQL Queries from Transactional Plugin Pipeline
2016-06-24 10:16
411 查看
Sometimes the LINQ, Query Expressions or Fetch just doesn't give you the ability to quickly query your data in the way you want to. A good example of this is the lack of left outer join support if you want a where clause to filter results based on the joined entity. Sometime, you just need to query your database using good old T-SQL. In CRM 4 you could do this fairly easily by simply opening a connection directly and doing what you need to.
Since Transactional Pipelines were introduced with CRM2011 , I've been dancing a jig every time I don't have write manual rollback compensation code – but – if you try a SQL query from a transactional pipeline that queries the same entity that you are updating/inserting, you'll get a blocking lock that will cause the operation to time out.
To get around this, you have a number of options:
1) Call the SQL from outside a transaction in the PreValidation or an Async Pipeline
2) Use the following method to hook into the CRM Transaction and execute your query from within that.
Note: I should say that this could be considered breaking the following rule in the SDK that defines what is supported or not:
"The use of application programming interfaces (APIs) other than the documented APIs in the Web services DeploymentService, DiscoveryService, Organization Data Service, SOAP endpoint for Web Resources and OrganizationService."
I'm assuming that you are familiar with the System.Data library, so I'm just posing how to get a SqlTransaction, and you can do the rest:
You can also call a stored procedure in a different database that points back to the MSCRM database if you have complex queries. You'll need to use 'SET TRUSTWORTHY ON' to ensure that the security context is passed between the two databases.
My advice would be to only use this method only where using the SDK is just not possible or performs too slowly.
Hope this helps.
from: http://www.develop1.net/public/post/SQL-Queries-from-Transactional-Plugin-Pipeline.aspx
Since Transactional Pipelines were introduced with CRM2011 , I've been dancing a jig every time I don't have write manual rollback compensation code – but – if you try a SQL query from a transactional pipeline that queries the same entity that you are updating/inserting, you'll get a blocking lock that will cause the operation to time out.
To get around this, you have a number of options:
1) Call the SQL from outside a transaction in the PreValidation or an Async Pipeline
2) Use the following method to hook into the CRM Transaction and execute your query from within that.
Note: I should say that this could be considered breaking the following rule in the SDK that defines what is supported or not:
"The use of application programming interfaces (APIs) other than the documented APIs in the Web services DeploymentService, DiscoveryService, Organization Data Service, SOAP endpoint for Web Resources and OrganizationService."
I'm assuming that you are familiar with the System.Data library, so I'm just posing how to get a SqlTransaction, and you can do the rest:
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext) serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext)); object platformContext = context.GetType().InvokeMember("PlatformContext", System.Reflection.BindingFlags.GetProperty, null, context, null); SqlTransaction tx = (SqlTransaction)platformContext.GetType().InvokeMember("SqlTransaction", System.Reflection.BindingFlags.GetProperty, null, platformContext, null); DataSet result = SqlHelper.ExecuteDataset(tx, CommandType.Text, "SELECT ...");
You can also call a stored procedure in a different database that points back to the MSCRM database if you have complex queries. You'll need to use 'SET TRUSTWORTHY ON' to ensure that the security context is passed between the two databases.
My advice would be to only use this method only where using the SDK is just not possible or performs too slowly.
Hope this helps.
from: http://www.develop1.net/public/post/SQL-Queries-from-Transactional-Plugin-Pipeline.aspx
相关文章推荐
- 解决The current branch is not configured for pull No value for key branch.master.merge found in config
- easyui的基本用法
- SSIS Passing Parameters to an ADO .NET Source query;向ado.net数据源传递参数。
- Color types not allowed (at ‘drawable’ with value ‘#f0f0f0’)
- UIsheji
- UIsheji
- UiSelector的官方帮助文档
- NGUI自适应
- 实例讲解iOS中的UIPageViewController翻页视图控制器
- iOS UIFont 字体名字大全
- EasyUI在表单提交之前进行验证的实例代码
- 背水一战 Windows 10 (18) - 绑定: 与 Element 绑定, 与 Indexer 绑定, TargetNullValue, FallbackValue
- 机器学习学习笔记 PRML Chapter 2.0 : Prerequisite之Sufficient Statistics
- Angular-ui-route
- ssm框架搭建+easyui增删改查实现
- android FragmentTabHost切换Fragment时避免UI重新加载
- arduino 学习笔记(一)
- QtQuick 全局变量解决ListView嵌套子ListView中访问子model难题
- 用代码创建UIPageControl时,无法显示的问题
- app控件获取之uiautomatorviewer