Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 .NET Inside SQL Server (2005)
 CLR that delete a file error

Author  Topic 

tengtium
Starting Member

5 Posts

Posted - 2009-01-27 : 04:13:56
need help badly.

i have this:

using System.IO;
using Microsoft.SqlServer.Server;
public partial class FileSystem
{
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static bool DeleteXmlFile(string xml_file)
{
System.IO.FileInfo obj_fso;
bool is_successfully_deleted = false;
obj_fso = new System.IO.FileInfo(xml_file);
if (obj_fso.Exists == true) {
obj_fso.Delete();
is_successfully_deleted = true;
}

return is_successfully_deleted;
}
}

and compile it:

csc /target:library /out:"C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Binn\StradSqlClrFileSystemDeleteXmlFile.dll" "c:\cs\FileSystemDeleteXmlFile.cs""

then execute this:

IF NOT EXISTS (SELECT * FROM sys.assemblies asms WHERE asms.name = N'StradSqlClrFileSystemDeleteXmlFile')
CREATE ASSEMBLY [StradSqlClrFileSystemDeleteXmlFile]
FROM 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Binn\StradSqlClrFileSystemDeleteXmlFile.dll'
WITH PERMISSION_SET = UNSAFE
GO

and this:

SELECT dbo.udf_delete_xml_file ("directory:\some_files")

and got this problem:

A .NET Framework error occurred during execution of user defined routine or aggregate 'udf_delete_xml_file':
System.UnauthorizedAccessException: Access to the path 'c:\myxmlfile.xml' is denied.
System.UnauthorizedAccessException:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileInfo.Delete()
at FileSystem.DeleteXmlFile(String xml_file)


please.. please help...

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2009-01-27 : 05:01:11
Looks like the account that this is running under doesn't have rights to that directory.
iirc, that's the SQL service account. Check the permissions on that directory.

--
Gail Shaw
SQL Server MVP
Go to Top of Page
   

- Advertisement -