Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 部署到SQL Server 2008时无法读取CLR函数中使用的文本文件_C#_Sql Server 2008_Clr_Sqlclr - Fatal编程技术网

C# 部署到SQL Server 2008时无法读取CLR函数中使用的文本文件

C# 部署到SQL Server 2008时无法读取CLR函数中使用的文本文件,c#,sql-server-2008,clr,sqlclr,C#,Sql Server 2008,Clr,Sqlclr,我在C#中创建了一个CLR用户定义函数,用于我的网站上的实现,并在SQLServer2008中部署了DLL 但是在读取文本文件后,字典返回为空 以下是部分代码,但问题在于读取文本文件: public Spelling() { try { string fileContent = File.ReadAllText("C:/Bgtext/big.txt"); List<string> wordList = f

我在C#中创建了一个CLR用户定义函数,用于我的网站上的实现,并在SQLServer2008中部署了DLL

但是在读取文本文件后,字典返回为空

以下是部分代码,但问题在于读取文本文件:

public Spelling()
{
        try
        {
            string fileContent = File.ReadAllText("C:/Bgtext/big.txt");
            List<string> wordList = fileContent.Split('\n').ToList();

            foreach (var word in wordList)
            {
                string trimmedWord = word.Trim().ToLower();
                if (_wordRegex.IsMatch(trimmedWord))
                {
                    if (_dictionary.ContainsKey(trimmedWord))
                        _dictionary[trimmedWord]++;
                    else
                        _dictionary.Add(trimmedWord, 1);
                }
            }
        }
        catch (Exception ex)
        {
            ex.Message.ToString();
        }

    }

    [SqlFunction(Name = "CorrectWords", DataAccess = DataAccessKind.Read)]
    public static string correctwords(string words)
    {
        string[] arr = words.Split(' ');

        for (int i = 0; i <= arr.Length - 1; i++)
        {
            arr[i] = Correct(arr[i]);
        }

        StringBuilder correctedwords = new StringBuilder();

        foreach (string value in arr)
        {
            correctedwords.Append(value);
            correctedwords.Append(' ');
        }

        return correctedwords.ToString();
    }
公共拼写()
{
尝试
{
字符串fileContent=File.ReadAllText(“C:/Bgtext/big.txt”);
List wordList=fileContent.Split('\n').ToList();
foreach(单词列表中的var单词)
{
字符串trimmedWord=word.Trim().ToLower();
if(_wordRegex.IsMatch(trimmedWord))
{
如果(_dictionary.ContainsKey(trimmedWord))
_字典[trimmedWord]++;
其他的
_字典。添加(trimmedWord,1);
}
}
}
捕获(例外情况除外)
{
例如Message.ToString();
}
}
[SqlFunction(Name=“CorrectWords”,DataAccess=DataAccessKind.Read)]
公共静态字符串更正字(字符串字)
{
字符串[]arr=words.Split(“”);

对于(int i=0;i您是否已授予数据库执行文件i/O的权限

下面是我的一个cls项目(显然是我的开发机器)的一个示例

还可以运行等效的alter assembly命令

要检查安全性,请添加一个hello world存储过程,类似于

  [SqlProcedure]
  public static void hello(SqlString yousaid)
  {
    if (yousaid.IsNull)
    {
      yousaid = "Not too chatty are you " + Environment.UserDomainName + "\\" + Environment.UserName;
      SqlContext.Pipe.Send("Mercedes Benz says, '" + yousaid.ToString() + "'\n");
    }
    else
    {
      SqlContext.Pipe.Send("The CLR proc says, you said '" + yousaid.ToString() + "'\n");
    }
  }
使用“要回显的内容”调用hello--确保它正常工作,然后使用null调用hello,并查看CLR运行时使用的安全上下文。然后可以验证文件的NT权限,等等


您还可以使用SqlContext.Pipe.Send方法作为显示调试信息的简单方式。

SQL Server进程是否也可以访问
C:/Bgtext/big.txt
?我尝试了所有三种权限集=安全、不安全、外部访问,但都不起作用。如何将对SQL Server的访问权限设置为文本文件,我尝试与everyone和will all access Permissions停止执行,您可能会从异常中获得有用的信息。如果这不能解决问题,您可能必须使用调试器(通常最简单)或日志详细信息对其进行解块,以允许您进行调试(如果没有运行调试器的权限)--存在很多可能性,也许您的正则表达式总是失败,谁知道呢。不能肯定;您还没有发布完整的代码(您甚至从未在sql方法中引用字典)当然,我们不能复制您的环境,如果它在一台机器上工作,而不是在另一台机器上工作,那么它显然是环境的。我在我的回答中添加了调试建议,太长了,不适合评论。也许您应该对该异常做些什么,而不是
ex.Message.ToString();
,这到底实现了什么?
  [SqlProcedure]
  public static void hello(SqlString yousaid)
  {
    if (yousaid.IsNull)
    {
      yousaid = "Not too chatty are you " + Environment.UserDomainName + "\\" + Environment.UserName;
      SqlContext.Pipe.Send("Mercedes Benz says, '" + yousaid.ToString() + "'\n");
    }
    else
    {
      SqlContext.Pipe.Send("The CLR proc says, you said '" + yousaid.ToString() + "'\n");
    }
  }