I have a method to remove string from file written in C# for SQL Server:
[SqlProcedure]
public static void fileReplace(string path, string patternToReplace, string stringForReplace)
{
using (SqlConnection connection = new SqlConnection("context connection=true"))
{
connection.Open();
try
{
File.WriteAllText(path, Regex.Replace(File.ReadAllText(path), patternToReplace, stringForReplace));
}
catch (Exception e)
{
}
}
}
Assembly and procedure in SQL Server are created like this:
CREATE ASSEMBLY FileUtils
FROM '<path>\<name>.dll'
WITH PERMISSION_SET = EXTERNAL_ACCESS
GO
CREATE PROCEDURE fileReplace (
@path nvarchar(max)
, @patterToReplace nvarchar(max)
, @stringForReplace nvarchar(max)
)
AS EXTERNAL NAME FileUtils.FileUtils.fileReplace;
GO
The method in C# is working correct, but when I call it from SQL Server, nothing happens (file doesn't change). What's wrong?
you SQLServer login needs to have permission to that file. It's possible that when it tries to read/write file it fails with exception, but exception supressed by try catch
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With