1 首先创建数据库,就不废话了。
fprintf格式2 建立ODBC数据源:控制面板->管理工具->ODBC数据源->用户DNS。如图
点击“添加”
旋转“SQL Server”,“完成”。
数据源名称--编程时要用到,可任意命名。服务器选择自己指定的
选择默认数据库,下一步
下一步
旋转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}=peName; %获取字段类型名
tableinfo{k,3}=peValue; %获取字段类型代码
tableinfo{k,4}=lumnWidth; %获取字段的宽度
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); %显示数据表中的数据
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论