Why does the import of nested namespaces from .NET assembly fails in MATLAB R2013a?

5 views (last 30 days)
When I am trying to execute my code in MATLAB or as executable I am getting the following error message:
Error :
Undefined function 'SqlConnection' for input arguments of type 'char'.
Error in Cekme (line 4)
MATLAB:UndefinedFunction
That “SqlConnection”command comes from “NET.addAssembly('System.Data');”. I guess that it is working on Matlab properly, because Matlab is line interpretted but executable is not.
conn = SqlConnection('Data Source= localhost; Initial Catalog=KalipZamanlari; Integrated Security=true');
asm = NET.addAssembly('System.Data');
import System.Data.SqlClient.*;
conn = SqlConnection('Data Source= localhost; Initial Catalog=KalipZamanlari; Integrated Security=true');

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 8 Apr 2013
It is a bug in MATLAB that if the .NET assembly has multiple nested namespaces, then the import statement, at first, only works with the outermost namespace.
Hence, in your case, the System.Data.dll has a namespace structure of:
System.Data.SqlClient.SqlConnection structure is:
namespace System.Data
{
namespace SqlClient
{
class SqlConnection
{
}
}
}
so when you used
asm = NET.addAssembly('System.Data');
import System.Data.SqlClient.*;
conn = SqlConnection('Data Source= localhost; Initial Catalog=KalipZamanlari; Integrated Security=true');
It failed because it was importing multiple namespaces (System.Data and SqlClient). However, the failure only occurs if the code has not been resaved. If it is edited and resaved (even with something trivial like removing and adding a semicolon), it starts working.
The workaround is to not use the import statement with multiple namespaces, so for example:
asm = NET.addAssembly('System.Data');
import System.Data.*;
conn = SqlClient.SqlConnection('Data Source= localhost; Initial Catalog=KalipZamanlari; Integrated Security=true');
will work. This issue has nothing to do with the DLL availability (where System.Data.dll is located). It is a GAC DLL and is always properly found by both MATLAB and MCR.
Please note that since now you will be importing only the outermost namespace, you will need to call each function with:
SqlClient.foo syntax

More Answers (0)

Products


Release

R2013a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!