I have a CLR to use the powershell to have access to the RichTextBox Object from .net.
On the SQL Function I have created like this:
CREATE FUNCTION [dbo].[FicheiroChines](@rtf [nvarchar](max), @path [nvarchar](500))
RETURNS
nvarchar(max)
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [abcSQLHelper].[abcSQLHelper.Chinese].TextFromRTF
Now need some help is to take out the @path as argument on the SQL Function (But still need it on the CLR) and inside the function to select a value from the table and send it to the CLR.
How can I achieve this? Can't find any info about this.
Edit: To try to help understanding what is going on here, I have a CLR in C# so I can be able to transform a RTF to string (example of the RTF below):
{\rtf1\ansi\ansicpg1252\deff0\deflang2070{\fonttbl{\f0\fmodern\fprq6\fcharset134 SimSun;}{\f1\fnil\fcharset0 MS Sans Serif;}} \viewkind4\uc1\pard\lang2052\f0\fs17\'b7\'f2\'c8\'cb\'b7\'eb\'b7\'f2\'c8\'cb6346\'b8\'f6\'ba\'ec\'b5\'c4\'b9\'f0\'bb\'a8\lang2070\f1 \par }
To be able to achieve this, since it has been asked to be done inside a SQL Query, I did have to put this in CLR in C# because of the chinese characters.
This is what is being send on the @rtf
.
Now the @path, is needed to be send also to the CLR that is done in C#, so he knows where to write the temp txt file (again need to write the output of the script on powershell to use the RichTextBox of .Net to transform the RTF).
Now the function is called in a query by doing this as ex: select dbo.RTF2Text(RTFText, (select filePath from tablexpto)) as teste from RTFTest
but has been asked to be only has select dbo.RTF2Text(RTFText) as teste from RTFTest
So in on first code, I need to select from there the path from the table, and need to send the argument inside the CLR
Hope that helped to clear what I need to be done, if not, I will try to improve it more
Edit 2:
I needed this because it was asking for me to do a function on SQL that could receive a RTF and return a string, the problem is the Chinese character, did try several things that I could find online and such but without success.
So I told to my self if I could have access to the RichTextBox from .Net I could do this more easy, so I do use the powershell (since I can't add WinForms Assembly to do this ... well I could but required alot more stuff) to access the Class RichTextBox of .Net and insert there the RTF and ask for the Text and put it into a file and read it from there (must be a file again because of the chinese caracters, if I just read the output comes as ?????)
This is the CLR that I have, sorry for not providing it at first.
public class Chinese
{
/// <summary>
/// It will receive the source of an RTF and transform into readable text
/// </summary>
/// <param name="rtf">Source of the RTF</param>
/// <param name="path">Directory where the file must be created</param>
/// <returns>Readable text from RTF</returns>
[Microsoft.SqlServer.Server.SqlFunction]
public static string TextFromRTF(string rtf, string path)
{
var output = String.Empty;
try
{
var proc = new Process();
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = "/C powershell.exe -ExecutionPolicy ByPass -Command Add-Type \"-AssemblyName System.Windows.Forms; $Rtb = New-Object -TypeName System.Windows.Forms.RichTextBox; $stringRTF = \\\"" + rtf + "\\\"; $Rtb.Rtf = $stringRTF; $Retorno = $Rtb.Text; Write-Output $Retorno > " + path + "\\chines.txt;\"";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
output = File.ReadAllText(path + "chines.txt");
}
catch (Exception ex)
{
return ex.Message;
}
return output;
}
}
So if I provide the RTF that i mention, it will return to me
夫人冯夫人6346个红的桂花
Edit 3:
After implementing the solution given by the write answer, I get this "Error"
Blockquote
Data access is not allowed in this context. Either the context is a function or method not marked with DataAccessKind.Read or SystemDataAccessKind.Read, is a callback to obtain data from FillRow method of a Table Valued Function, or is a UDT validation method.
Did add this to the CLR:
using (var con = new SqlConnection("Context Connection = true;"))
{
using (var cmd = con.CreateCommand())
{
cmd.CommandText = "select @FilePath = filePath from dbo.tablexpto;";
var sqlParam = new SqlParameter("@FilePath", SqlDbType.NVarChar, 500)
{
Direction = ParameterDirection.Output
};
con.Open();
cmd.ExecuteNonQuery();
if (sqlParam.Value != DBNull.Value)
{
path = sqlParam.Value.ToString();
}
}
}
select filePath from tablexpto
and remove that parameter from the UDF. See Data Access from CLR Database Objects.