Excuting Matlab code inside SQL Server 2008 R2
2 views (last 30 days)
Show older comments
I want to run matlab code using a User-defined function in SQL Server 2008 R2 (Express edition - 64bits). I do not know if this is possible...
I Execute the following script to be able to load the MWArray.dll assembly onto the SQL Server instance.
ALTER DATABASE [TEST_MATLAB] SET TRUSTWORTHY ON;
CREATE ASSEMBLY [system.drawing]
FROM 'C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll'
WITH PERMISSION_SET = UNSAFE;
CREATE ASSEMBLY [MWArray]
from 'C:\Program Files\MATLAB\MATLAB Compiler Runtime\v80\toolbox\dotnetbuilder\bin\win64\v2.0\MWArray.dll'
WITH PERMISSION_SET = UNSAFE;
This works well.
I then create in Visual Studio 2010 a "Visual C# SQL CLR Database Project" to define a user-defined function. Here is the code of the (test) function:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using MathWorks.MATLAB.NET.Arrays;
public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString Function1()
{
MWNumericArray a = new MWNumericArray(56);
return new SqlString(a[1].ToString());
//return new SqlString("Hello");
}
};
I then deployed the SQL CLR project using VS2010 "deploy" functionality. This loads the project's assembly in the database.
When I tried to execute the function on the SQL Server database using
SELECT dbo.Function1()
I get the error:
Msg 6522, Level 16, State 2, Line 1
A .NET Framework error occurred during execution of user-defined routine or aggregate "Function1":
System.TypeInitializationException: Une exception a été levée par l'initialiseur de type pour 'MathWorks.MATLAB.NET.Arrays.MWNumericArray'. ---> System.TypeInitializationException: Une exception a été levée par l'initialiseur de type pour 'MathWorks.MATLAB.NET.Arrays.MWArray'. ---> System.TypeInitializationException: Une exception a été levée par l'initialiseur de type pour 'MathWorks.MATLAB.NET.Utility.MWMCR'. ---> System.BadImageFormatException: Tentative de chargement d’un programme de format incorrect. (Exception de HRESULT : 0x8007000B)
System.BadImageFormatException:
à MathWorks.MATLAB.NET.Utility.MWMCR.mclmcrInitialize2(Int32 primaryMode)
à MathWorks.MATLAB.NET.Utility.MWMCR..cctor()
System.TypeInitializationException:
à MathWorks.MATLAB.NET.Arrays.MWArray..cctor()
System.TypeInitializationException:
à MathWorks.MATLAB.NET.Arrays.MWNumericArray.get__Inf()
à MathWorks.MATLAB.NET.Arrays.MWNumericArray..cctor()
System.TypeInitializationException:
à MathWorks.MATLAB.NET.Arrays.MWNumericArray..ctor(Int32 scalar)
à UserDefinedFunctions.Function1()
What could cause this error ? I tried to compile the SQL CLR project using .NET 3.5 and targetting "Any CPU" or "x64". Notice that If I load the 32-bit version of MWArray.dll in the database. It gives the following errors when I "deploy" the project from VS2010 (i.e. when I load the project's DLL on the database):
Creating [SqlServerProjec2]...
C:\MatlabCode\TESTSQLCLR\ConsoleApplication1\SqlServerProjec2\bin\Debug\SqlServerProjec2.sql(37-37): Deploy error SQL01268: .Net SqlClient Data Provider: Msg 6503, Level 16, State 12, Line 1 Assembly 'mwarray, version=2.12.1.0, culture=neutral, publickeytoken=e1d84a0da19db86f.' was not found in the SQL catalog.
An error occurred while the batch was being executed.
It seems like it does not recognize the 32-bit MWArray.dll ...
Thanks for you help!
0 Comments
Answers (0)
See Also
Categories
Find more on Deploy to .NET Applications Using MWArray API in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!