Call ADO.NET from MATLAB

You can call .NET from MATLAB, so I thought I'd try to use ADO.NET to connect to the database.

I seem to run into a lock problem - anytime when you try to create a Command object, it causes an error.

You can try it yourself:

>> NET.addAssembly('System.Data');
>> sqlconn = System.Data.SqlClient.SqlConnection();
>> sqlconn.State

ans = 

    Closed    

>> % So far, so good
>> sqlcmd = System.Data.SqlClient.SqlCommand();
??? Error using ==> System.Data.SqlClient.SqlCommand
'Connection' is already defined as a property.

>> 

Does anyone have an understanding of this? It seems like a clean and simple error in the MATLAB part - perhaps this happens with every .NET class that has a property called "Connection".

Should I just drop the towel and stop using MATLAB to talk to the database using .NET?


Answer (thanks to Fazil's research) : upgrade MATLAB to a version larger than 2009a .

+3
3

MATLAB. MATLAB ?

>> version

ans =

7.9.1.705 (R2009b) Service Pack 1

>> NET.addAssembly('System.Data');
sqlconn = System.Data.SqlClient.SqlConnection();
sqlconn.State
sqlcmd = System.Data.SqlClient.SqlCommand()

ans = 

    Closed    


sqlcmd = 

  System.Data.SqlClient.SqlCommand handle
  Package: System.Data.SqlClient

  Properties:
                Connection: []
    NotificationAutoEnlist: 1
              Notification: []
               Transaction: []
               CommandText: [1x1 System.String]
            CommandTimeout: 30
               CommandType: [1x1 System.Data.CommandType]
         DesignTimeVisible: 1
                Parameters: [1x1 System.Data.SqlClient.SqlParameterCollection]
          UpdatedRowSource: [1x1 System.Data.UpdateRowSource]
                      Site: []
                 Container: []

  Methods, Events, Superclasses

>> 
+2
NET.addAssembly('System.Data');
sqlconn = System.Data.SqlClient.SqlConnection();
sqlcmd = sqlconn.CreateCommand();
sqlcmd.CommandText = "SELECT count(id) FROM sometable";
sqlconn.Open();
sqlrdr = sqlcmd.ExecuteReader();
sqlrdr.Read();
sqlrdr.GetInt64(0)
+3

MATLAB, .NET?

, , Java MATLAB, , JDBC.

, Class.forName(), , MATLAB javaclasspath, char(), :

// MatlabDBAdapter.java

import java.sql.*;

public class MatlabDBAdapter {

    public void loadDriver(String driverClass) throws ClassNotFoundException
    {
        Class.forName(driverClass);
    }
    public Connection getConnection(String dburl) throws SQLException
    {
        return DriverManager.getConnection(dburl);
    }
}

m :

% dbexample.m
% adapted from "getting started" section
% of http://www.zentus.com/sqlitejdbc/ 

% replace the following two lines with 
%    1. where you put the compiled MatlabDBAdapter, 
%    2. also where you put the driver jar file


javaaddpath('c:/appl/java/project/MatlabDBAdapter/bin');
javaaddpath('c:/appl/java/common/sqlitejdbc-v056.jar');

dba=com.example.test.database.MatlabDBAdapter();
dba.loadDriver('org.sqlite.JDBC');
conn=dba.getConnection('jdbc:sqlite:test.db');

disp ('Adding data....');   

stat = conn.createStatement();
stat.executeUpdate('drop table if exists people;');
stat.executeUpdate('create table people (name, occupation);');
prep = conn.prepareStatement(...
    'insert into people values (?, ?);');

prep.setString(1, 'Gandhi');
prep.setString(2, 'politics');
prep.addBatch();
prep.setString(1, 'Turing');
prep.setString(2, 'computers');
prep.addBatch();
prep.setString(1, 'Wittgenstein');
prep.setString(2, 'smartypants');
prep.addBatch();

conn.setAutoCommit(false);
prep.executeBatch();
conn.setAutoCommit(true);

disp ('Reading back data....');

rs = stat.executeQuery('select * from people;');
while (rs.next()) 
    % need to explicitly convert java.lang.String using char()
    disp(['name = ' char(rs.getString('name'))]);
    disp(['job = ' char(rs.getString('occupation'))]);
end
rs.close();
conn.close();
+3

Source: https://habr.com/ru/post/1737992/


All Articles