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
Best Answer