好的,这有点复杂,所以我会尽量简化它。我有一个带有替代触发器的 SQL Server 视图。触发器调用 CLR UDF (C#),将其称为 MyUDF。MyUDF 尝试使用文字连接字符串打开第二个连接,即
using (SqlConnection connection2 = new SqlConnection(newConnString)) { ...
我已经确认连接字符串是好的。我还确认 UDF 在更新触发器之外可以正常工作。但是,当被更新触发器调用时,我得到如下所示的 DotNet 错误。我什至尝试根据这个 KB 启用 DTC,但没有成功: //msdn.microsoft.com/en-us/library/dd327979.aspx
由于权限,我需要打开第二个连接。当前连接可能没有足够的权限来执行必要的查询,所以我不能使用"context connection=true"
问题:有没有办法在这种情况下打开第二个连接?
这是 DotNet 错误(SQL Server 2008 Standard、Windows 2008 x86、DotNet 4):
A .NET Framework error occurred during execution of user-defined routine or aggregate "MyUDF":
System.Data.SqlClient.SqlException: Transaction context in use by another session.
System.Data.SqlClient.SqlException:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)
at System.Data.SqlClient.SqlInternalConnectionTds.PropagateTransactionCookie(Byte[] cookie)
at System.Data.SqlClient.SqlInternalConnection.EnlistNonNull(Transaction tx)
at System.Data.SqlClient.SqlInternalConnection.Enlist(Transaction tx)
at System.Data.SqlClient.SqlInternalConnectionTds.Activate(Transaction transaction)
at System.Data.ProviderBase.DbConnectionInternal.ActivateConnection(Transaction transaction)
at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at UserDefinedFunctions.MyUDF(SqlString data)