如何从Matlab连接到Microsoft SQL Server 2008(MSSQL)? [英] How to connect to Microsoft SQL Server 2008 (MSSQL) from Matlab?
问题描述
这可能是一个简单的问题:
This is probably a simple question:
- 如何从Matlab连接到Microsoft SQL Server 2008 R2?
- 在进行一些SQL查询后,如何将表读入矩阵?
更新
我宁愿不需要使用ODBC手动设置的方法.
I'd prefer a method that doesn't require use of manual setup using ODBC.
推荐答案
我在下面对访问MATLAB中数据库的不同方法进行了概述.这是堆栈溢出问题的列表,其中讨论了其中的一些问题:
I present below a review of the different approaches for accessing databases in MATLAB. Here is a list of Stack Overflow questions where some of them were discussed:
- 如何在没有matlabs数据库工具箱的情况下从matlab访问PostgreSQL数据库?
- 连接MATLAB 7.0和MYSQL
- 传达MATLAB SQL Server
- 使用Matlab获取Access数据库表的名称
- 从MATLAB调用ADO.NET
- How can I access a postgresql database from matlab with without matlabs database toolbox?
- connection of MATLAB 7.0 and MYSQL
- communicate MATLAB SQL Server
- Getting names of Access database tables with Matlab
- Invoking ADO.NET from MATLAB
MATLAB具有嵌入式Java JVM,使您可以直接调用 JDBC驱动程序.您首先需要在Java classpth 在MATLAB中:
MATLAB have an embedded Java JVM, allowing you to directly call the JDBC drivers from MATLAB. You first need to make them available on the Java classpth in MATLAB:
javaclasspath('sqljdbc4.jar');
%# load driver and create connection
driver = com.microsoft.sqlserver.jdbc.SQLServerDriver;
conn = driver.connect('jdbc:sqlserver://<HOST>:<PORT>;databaseName=<DB>');
%# query database
q = conn.prepareStatement('select * from <TABLE>');
rs = q.executeQuery();
while rs.next()
char(rs.getString(0))
end
rs.close();
conn.close();
数据库工具箱
如果您可以访问数据库工具箱,则可以简化上面的内容,因为它充当了JDBC/ODBC内容的包装器:
Database Toolbox
If you have access to the Database Toolbox, it can simplify the above as it acts as a wrapper around JDBC/ODBC stuff:
conn = database('<DB>', '<USER>','<PASS>', ...
'com.microsoft.sqlserver.jdbc.SQLServerDriver', ...
'jdbc:sqlserver://<HOST>:<PORT>;database=<DB>');
curs = exec(conn, 'select * from <TABLE>');
curs = fetch(curs);
curs.Data
close(curs)
close(conn)
您还可以通过ODBC访问数据库.首先创建一个MSN Server的DSN(Control Panel > ODBC Data Sources
),然后从数据库工具箱中使用它:
You can also access the database through ODBC. First create a DSN to MSSQL Server (Control Panel > ODBC Data Sources
), then use it from the Database Toolbox:
conn = database('myDB', '', ''); %# User/System DSN
%...
close(conn)
COM
您可以直接使用MATLAB中的ADO OLEDB组件.一种方法是指定连接字符串(无DNS):
COM
You can directly use the ADO OLEDB component from MATLAB. One way is to specify a connection string (DNS-less):
conn = actxserver('ADODB.Connection');
conn.Open('Provider=sqloledb;Data Source=<HOST>;Initial Catalog=<DB>;User Id=<USER>;Password=<PASS>;');
conn.Execute('select * from <TABLE>').GetRows
conn.Close()
.NET
最后,最新版本的MATLAB添加了从MATLAB调用.NET .因此,您可以使用ADO.NET数据提供程序:
.NET
Finally, recent versions of MATLAB added the ability to call .NET from MATLAB. So you can use the ADO.NET data providers:
import System.Data.SqlClient.*
NET.addAssembly('System.Data');
conn = SqlConnection('Data Source=<HOST>;Initial Catalog=<DB>');
conn.Open();
q = SqlCommand('select * from <TABLE>', conn);
r = q.ExecuteReader();
while r.Read()
char(r.GetString(0))
end
r.Close()
conn.Close()
这篇关于如何从Matlab连接到Microsoft SQL Server 2008(MSSQL)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!