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
 Transact-SQL (2005)
 CLR to function or sproc

Author  Topic 

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-11-16 : 19:00:45
Hi there,

Given the following C# function, how can it be implemented in a SQL function or stored procedure ? I was able to create the dll and created the assembly, but how do I create a function or a sproc based on the assembly. I can't get past this


CREATE function translate
AS
EXTERNAL NAME Translator.TranslateText


using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace Utilities
{
public static class Translator
{
/// <summary>
/// Translates the text.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="languagePair">The language pair.</param>
/// <returns></returns>
public static string TranslateText(string input, string languagePair)
{
return TranslateText(input, languagePair, System.Text.Encoding.UTF7);
}

/// <summary>
/// Translate Text using Google Translate
/// </summary>
/// <param name="input">The string you want translated</param>
/// <param name="languagePair">2 letter Language Pair, delimited by "|".
/// e.g. "en|da" language pair means to translate from English to Danish</param>
/// <param name="encoding">The encoding.</param>
/// <returns>Translated to String</returns>
public static string TranslateText(string input, string languagePair, Encoding encoding)
{
string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);

string result = String.Empty;

using (WebClient webClient = new WebClient())
{
webClient.Encoding = encoding;
result = webClient.DownloadString(url);
}

Match m = Regex.Match(result, "(?<=<div id=result_box dir=\"ltr\">)(.*?)(?=</div>)");

if (m.Success)
result = m.Value;

return result;
}
}
}


<><><><><><><><><><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-16 : 19:11:15
Here's an example:

CREATE PROCEDURE [dbo].[isp_DatabaseGrowth] @srvName [nvarchar](4000)
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [DatabaseGrowth].[StoredProcedures].[isp_DatabaseGrowth]
GO

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-16 : 19:12:09
That's taken directly from my blog where I present a CLR object that DBAs might find useful. You may want to follow the T-SQL code to see what's all needed beyond the dll.

http://weblogs.sqlteam.com/tarad/archive/2008/12/16/How-to-track-database-growth-across-multiple-SQL-Server-instances.aspx

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-11-16 : 19:29:30
tko! but problem is the C# code I am using has two identical TranslateText methods. I guess this is not supported. I assume it means I will have to rename one of them?

Thanks!

<><><><><><><><><><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-16 : 19:46:20
I'm not sure. I'll defer to someone with more .NET experience.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-11-17 : 12:47:11
ok got this thing working in case someone wants it here it is
(the big catch someone told me is that Google will block the ip from which this is being called if you do it too many times)

1. Create following translate.cs file

using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

public static class Translator
{
/// <summary>
/// Translates the text.
/// </summary>
/// <param name="input">The input.</param>
/// <param name="languagePair">The language pair.</param>
/// <returns></returns>
public static void Main(string[] args)
{
TranslateText(args[1], args[2]);
}

/// <summary>
/// Translate Text using Google Translate
/// </summary>
/// <param name="input">The string you want translated</param>
/// <param name="languagePair">2 letter Language Pair, delimited by "|".
/// e.g. "en|da" language pair means to translate from English to Danish</param>
/// <param name="encoding">The encoding.</param>
/// <returns>Translated to String</returns>
public static string TranslateText(string input, string languagePair)
{
string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);

string result = String.Empty;

using (WebClient webClient = new WebClient())
{
webClient.Encoding = System.Text.Encoding.UTF7;
result = webClient.DownloadString(url);
}

Match m = Regex.Match(result, "(?<=<div id=result_box dir=\"ltr\">)(.*?)(?=</div>)");

if (m.Success)
result = m.Value;

return result;
}
}


2. run the following utility for c# that creates a dll. csc /target:library [LOCATION_OF_translate.cs ]:\translate.cs
3. Copy the translate.dll to a location where the security context you are running as has full rights to
4.

CREATE ASSEMBLY translate from '[LOCATION_WHERE_YOU_COPIED_translate.dll]:\translate.dll' WITH PERMISSION_SET = EXTERNAL_ACCESS


CREATE FUNCTION [dbo].[translate]
(@input nvarchar(4000), @languagePair nvarchar(4000)
)
RETURNS nvarchar(max)
WITH EXECUTE AS CALLER
EXTERNAL NAME [translate].[Translator].[TranslateText]
GO

SELECT [dbo].[translate] (
'Hello beautiful French lady. Could I have this dance with you?'
,'EN|FR')


<><><><><><><><><><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page
   

- Advertisement -