从MATLAB调用ADO.NET

时间:2010-03-22 06:49:45

标签: ado.net matlab

可以从MATLAB调用.NET,所以我想我会尝试使用ADO.NET连接到数据库。

我似乎遇到了阻塞问题 - 无论何时尝试创建Command对象,都会抛出错误。

您可以自己试试:

>> 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.

>> 

有没有人对此有所了解?对于MATLAB来说,这似乎是一个纯粹而简单的错误 - 也许它发生在每个.NET类碰巧都有一个名为“Connection”的属性。

我是否应该放弃并放弃使用MATLAB与使用.NET的数据库交谈?


回答(感谢Fazil的调查):将MATLAB升级到大于2009a的版本

3 个答案:

答案 0 :(得分:3)

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)

答案 1 :(得分:3)

  

我是否应该放弃并放弃使用MATLAB与使用.NET的数据库交谈?

不,但是意识到你也可以使用MATLAB中的Java,如果你熟悉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-file:

% 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();

答案 2 :(得分:2)

我无法在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

>>