Answer the question
In order to leave comments, you need to log in
How to fix System.Data.SqlClient.SqlException: Transaction context in use by another session error?
task:
on the sql side, a transaction is opened inside which something happens.
inside this transaction, you need to call the clr procedure in C #, which will write something somewhere
, and if the transaction is rolled back, the actions that were performed during the operation of the clr function
should not be rolled back.
for the test, I threw a method in C #
namespace Future.SQLCLR
{
public class Exception
{
public static string ConnectionString
{
get
{
//string ret = "context connection = true";//
string ret = "Data Source = server; Initial Catalog = database; Integrated Security = True";
return ret;
}
}
[SqlFunction()]
public static void TestInsert(SqlString inputParam)
{
using (var cn = new SqlConnection(ConnectionString))
{
try
{
cn.ConnectionString = "Enlist=false";
cn.Open();
var cmd = new SqlCommand("insert into table(field)" +
"select field from table_1", cn);
cmd.ExecuteNonQuery();
}
catch (System.Exception)
{
throw;
}
finally
{
cn.Close();
}
}
}
}
.......
}
CREATE PROCEDURE [audit].[TestInsert]
(
@msg nvarchar(2048)
)
AS EXTERNAL NAME CLR.[SQLCLR.Exception].TestInsert
begin tran
exec [TestInsert] 'msg'
rollback
Answer the question
In order to leave comments, you need to log in
Confuses the description of the stored procedure - is it some part of it? Or what is it?
CREATE PROCEDURE [dbo].[MyPROC]
(
@Param1 nvarchar(2048) out,
@Param2 int out,
@Param3 int out,
@Param4 int
)
AS EXTERNAL NAME DWH_CLR.[Future.SQLCLR.Exception].HandleException
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question