`
deepfuture
  • 浏览: 4321292 次
  • 性别: Icon_minigender_1
  • 来自: 湛江
博客专栏
073ec2a9-85b7-3ebf-a3bb-c6361e6c6f64
SQLite源码剖析
浏览量:79303
1591c4b8-62f1-3d3e-9551-25c77465da96
WIN32汇编语言学习应用...
浏览量:68103
F5390db6-59dd-338f-ba18-4e93943ff06a
神奇的perl
浏览量:101192
Dac44363-8a80-3836-99aa-f7b7780fa6e2
lucene等搜索引擎解析...
浏览量:280262
Ec49a563-4109-3c69-9c83-8f6d068ba113
深入lucene3.5源码...
浏览量:14535
9b99bfc2-19c2-3346-9100-7f8879c731ce
VB.NET并行与分布式编...
浏览量:65153
B1db2af3-06b3-35bb-ac08-59ff2d1324b4
silverlight 5...
浏览量:31187
4a56b548-ab3d-35af-a984-e0781d142c23
算法下午茶系列
浏览量:45069
社区版块
存档分类
最新评论

matlab与数据库连接

 
阅读更多
Matlab与数据库连接

 

 

 

 

 

 

1 首先创建数据库,就不废话了。

2 建立ODBC数据源:控制面板->管理工具->ODBC数据源->用户DNS。如图

 



点击“添加”



 选择“SQL Server”,“完成”。

 




数据源名称--编程时要用到,可任意命名。服务器选择自己指定的

 










3 编程(来自网络)这段程序已经过验证。

sourceName=input('Enter the source Name:','s');       %获取数据源的名称(dbtest)

Timeout=logintimeout(5);                         %允许登录连接时间最长为5s

conn=database(sourceName,'sa','123');                    %获取数据库连接对象

ping(conn)                                     %测试数据库连接状态

dbmeta=dmd(conn);                           %获取数据元对象

t=tables(dbmeta,'tutorial');                      %获取cata为tutorial的表名

[trow,tcolumn]=size(t);                         %获取返回数组的大小

index=1;                               

for i=1:trow                                  %由于表中既包含了系统表格

    if strcmp(t{i,2},'TABLE')                   %又包含了用户表格,需要在其中

        tablename{1,index}=t{i,1};             %找出用户表格,对t数组的每一行

        index=index+1;                       %的第二个元素判断是table则为用

    end                                     %户表。

end

 

 

tabletosee=input('Which one would you want to use? ','s'); %获取欲查看的表格的名称
    sql=['select * from ',tabletosee];                   %构造查询的sql语句
    curs=exec(conn,sql);                            %执行该sql语句
    setdbprefs('DataReturnFormat','cellarray');           %设定数据返回格式
    curs=fetch(curs);                               %获取结果集对象
    numrows=rows(curs);                           %获取返回数据的行数
    numcols=cols(curs);                             %获取返回数据的列数
disp('--------------------------------------------------------------'); %在屏幕中显示表格信息
   fprintf('          Information of Table %s . ',tabletosee);
    disp('--------------------------------------------------------------');
    fprintf('number of rows=%d, number of columns=%d ',numrows,numcols);
    disp('    FieldName     typeName typeValue columnWidth nullable');
for k=1:numcols                                 %分别获取相关信息
        attributes=attr(curs,k);
  
        tableinfo{k,1}=attributes.fieldName;              %获取字段名称
        tableinfo{k,2}=attributes.typeName;              %获取字段类型名
        tableinfo{k,3}=attributes.typeValue;              %获取字段类型代码
        tableinfo{k,4}=attributes.columnWidth;           %获取字段的宽度
        tableinfo{k,5}=attributes.nullable;               %获取字段是否可空
    end
disp(tableinfo);                                       %显示数据表的结构信息
    disp('-------------------------------------------------------------');
    fprintf('          Data of Table %s . ',tabletosee);
    disp('--------------------------------------------------------------');
    for i=1:numcols
        fprintf(' %s',tableinfo{i,1});
    end
    fprintf(' ');
    tabledata=curs.data;                               %获取结果集对象的数据
disp(tabledata);                                  %显示数据表中的数据

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics