1

我正在编写一个 CLR 函数来解析表列并将结果写入另一个表。基本要求是解析Detail列,其中包含一个Time部分和一个ID部分。结果将是两个 ID 之间的时间差。

例如:Time1,Id1;Time2,Id2;Time3,Id3... 等等 Time2-Time1 是 Id1 所用的时间,以秒为单位。

相同的函数在普通控制台应用程序中工作,但是当我从 SQL 服务器调用它时,CLR 函数抛出异常。错误是

在执行用户定义的例程或聚合“Function1”期间发生 .NET Framework 错误:System.Security.HostProtectionException:尝试执行 CLR 主机禁止的操作。受保护的资源(仅在完全信任的情况下可用)是:所有所需的资源是:UI System.Security.HostProtectionException:在 UserDefinedFunctions.Function1(String msisdn, String promptdetails)

我的代码是:SqlConnection conn = new SqlConnection(); SqlCommand cmd = 新的 SqlCommand();

    int count = 0;
    string PromptPart = string.Empty;
    string PrevPromptPart = string.Empty;
    DateTime TimePart;
    TimePart = new DateTime();
    DateTime PrevTimePart;
    PrevTimePart = new DateTime();
    TimeSpan difference;

    try
    {
        count++;
        conn.ConnectionString = "Context Connection=true";
        cmd.Connection = conn;

        String[] string1 = promptdetails.Split(";".ToCharArray());
        foreach (var item1 in string1)
        {
            count++;
            String[] string2 = item1.Split(",".ToCharArray());
            PromptPart = string2[1];
            TimePart = DateTime.ParseExact(string2[0], "M/d/yyyy h:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);
            if (count > 1)
            {
                StringBuilder sbQuery = new StringBuilder(1024);
                sbQuery.Append("INSERT INTO [Shami].[DBO].[data] (MSISDN,PromptID1,PromptID2,TimeDifference) VALUES");
                sbQuery.Append("('");
                sbQuery.Append(msisdn);
                sbQuery.Append("',");
                difference = TimePart.Subtract(PrevTimePart);
                sbQuery.Append("'");
                sbQuery.Append(PrevPromptPart);
                sbQuery.Append("','");
                sbQuery.Append(PromptPart);
                sbQuery.Append("',");
                sbQuery.Append(difference.Seconds);
                sbQuery.Append(")");
                string sub = string.Empty;
                sub = sbQuery.ToString();
                try
                {
                    conn.Open();
                    cmd = new SqlCommand(sub);
                    SqlContext.Pipe.ExecuteAndSend(cmd);
                }
                catch (Exception ie)
                {
                    Console.WriteLine("Error..");
                }
            }
            if (count <= 1)
            {
                StringBuilder sbQuery = new StringBuilder(1024);
                sbQuery.Append("INSERT INTO [Shami].[DBO].[data] (MSISDN,PromptID1,PromptID2,TimeDifference) VALUES");
                sbQuery.Append("('");
                sbQuery.Append(msisdn);
                sbQuery.Append("',");
                sbQuery.Append("'_'");
                sbQuery.Append(",");
                sbQuery.Append(PromptPart);
                sbQuery.Append(",");
                sbQuery.Append("'0'");
                sbQuery.Append(")");
                string sub = string.Empty;
                sub = sbQuery.ToString();
                try
                {
                    conn.Open();
                    cmd = new SqlCommand(sub);
                    SqlContext.Pipe.ExecuteAndSend(cmd);
                }
                catch (Exception ie)
                {
                    Console.WriteLine("Error..");
                }
            }
            PrevPromptPart = PromptPart;
            PrevTimePart = TimePart;
        }



    }
    catch (Exception)
    { ;}
    finally
    {
        conn.Close();
        conn.Dispose();
        cmd.Dispose();
    }
    return msisdn;

请让我知道我哪里出错了。我无法在 CLR 中调试。

4

1 回答 1

1

你只需要更换你的线路

Console.WriteLine("错误...");

用这条线:

SqlContext.Pipe.Send("错误...");

这将为您做同样的事情,它会运行

于 2015-02-02T00:38:25.327 回答