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);                                  %显示数据表中的数据

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值