Is it possible to use resource assemblies (* .resources.dll) in a SQL-CLR project? If so, how?
I am writing one SQL-CLR procedure in which I want to explicitly specify the language in which the output data is generated through the input parameter.
I figured it out myself, so I present my decision:
I did not succeed in adding resources to the SQL-CLR project, so we create a separate .net project - a simple library assembly, for example, MyDatabaseResources.
Add to it the necessary resources in different languages - files * .resx. I have this for example files: myres.resx (by default), myres.en.resx, myres.ru.resx .., etc.
During the project build, the main library is created and in the subdirectories en, de, ru .. satellite assemblies - "MyDatabaseResources.resources.dll". On the SQL server there is no concept of a subdirectory, so you need to merge all the files into one category, with the satellite assemblies renamed according to the following principle: MyDatabaseResources.resources.en.dll, MyDatabaseResources.resources.de.dll, MyDatabaseResources.resources.ru.dll ..
In the database project we add links to all resource project files (main file and satellite). At the same time, satellite assemblies should be added one by one, while they should be renamed accordingly, because the studio defines them under their original name "MyDatabaseResources.resources.dll". To do this, in the properties of the file "Assemblyname" of "MyDatabaseResources.resources.dll" do "MyDatabaseResources.resources.EN.dll". Under this name, the assembly will be registered on the SQL server. But in the studio in the list of referenced assemblies, this file will remain under the old name. To be able to add the following satellite assemblies, this link should also be renamed. To do this, open the project file "* .sqlproj", find the tag
.. Reference Include="MyDatabaseResources.resources.dll" ..
which refers to the build "EN" and rename the link to
.. Reference Include="MyDatabaseResources.resources.EN.dll" ..
After the project is overloaded in the studio, the names will be displayed correctly.
After creating the project, all necessary assemblies will be registered in the SQL server database. A procedure that uses localized resources might look like this:
[Microsoft.SqlServer.Server.SqlProcedure] public static SqlInt32 SqlStoredProcedure1(SqlString lang) { if (lang.ToString() == "de") { System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de-DE"); System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-DE"); } else if (lang.ToString() == "fr") { System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-FR"); System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("fr-FR"); } else if (lang.ToString() == "ru") { System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("ru-RU"); System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ru-RU"); } else if (lang.ToString() == "" /*default*/) { System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.InvariantCulture; } SqlContext.Pipe.Send(lang.ToString()); SqlContext.Pipe.Send(System.Threading.Thread.CurrentThread.CurrentCulture.Name); SqlContext.Pipe.Send(MyDatabaseResources.Resource1.a); return new SqlInt32(0); }
Source: https://ru.stackoverflow.com/questions/500608/
All Articles