个人觉得,数据库操作连接和操作上手很快,但是那些类型总是会让你头疼 目前我还没搞清楚用怎么从t-sql 的decimal技术到mfc中相应的数据?? 将一下数据库连接的步骤 一:加载动态链接库 #import c:\program files\common files\system\ado\msado15.dll \ no
个人觉得,数据库操作连接和操作上手很快,但是那些类型总是会让你头疼
目前我还没搞清楚用怎么从t-sql 的decimal技术到mfc中相应的数据??
将一下数据库连接的步骤
一:加载动态链接库
#import c:\program files\common files\system\ado\msado15.dll \
no_namespace rename(eof, endoffile)
讲一下这句话什么意思,就是导入动态链接库,否则你的那个什么ptrconn,ptrrecord连接指针,都会在编译的时候报错
第二个rename,eof替换成,endoffile是为了在从记录集recordset取出来时候,判断是否到达了结尾
如果是到达了结尾,那么就会返回非variant_false
二:声明连接指针和记录集
_connectionptr ptrconn; // 定义connection对象
_recordsetptr ptrrecord;
三:创建连接
coinitialize(null); //不要忘记了,否则指针全部为空,无效
try//打开连接 { // 创建一个连接实体 ptrconn.createinstance(__uuidof(connection)); // 设定连接等待的最大秒数,默认是15秒 ptrconn->connectiontimeout = 20; // 打开连接 ptrconn->open(driver={sql server};server=127.0.0.1;uid=laicb;pwd=616458;database=dbcourse, ,//登录用户名 ,//登录密码 adconnectunspecified);//打开连接 } catch(_com_error &e)--捕获异常 { cstring str; cstring strtemp; str.format(text(error:\n)); strtemp.format(text(code = %08lx\n), e.error()); str+=\n; str+=strtemp; strtemp.format(text(meaning = %s\n), e.errormessage()); str+=\n; str+=strtemp; strtemp.format(text(source = %s\n), (wchar_t*) e.source()); str+=\n; str+=strtemp; strtemp.format(text(description = %s\n), (wchar_t*) e.description()); str+=\n; str+=strtemp; messagebox(str); }
四:打开记录集,初始化记录集
try { //_recordsetptr ptrrs; // recordset 对象 // 创建recordset 对象实体 ptrrecord.createinstance(__uuidof(recordset));//有些时候如果记录级背使用过了,可能需要重新创建实例,然后再打开 ptrrecord->open(select * from dbo.staffinfo,///为什么把这里的数据库改成dbo.staffrecord时候,列表框就显示全为空 ptrconn.getinterfaceptr(), adopenkeyset, //注意在vb说明文档时候,首字母a是大写的,在c++中应该小写 adlockbatchoptimistic, adcmdtext); //int j= ptrrecord->recordcount; //或者 //ptrrs = ptrconn ->execute(m_ strsql,null, adcmdtext); } catch(_com_error &e) { cstring str; cstring strtemp; str.format(text(error:\n)); strtemp.format(text(code = %08lx\n), e.error()); str+=\n; str+=strtemp; strtemp.format(text(meaning = %s\n), e.errormessage()); str+=\n; str+=strtemp; strtemp.format(text(source = %s\n), (wchar_t*) e.source()); str+=\n; str+=strtemp; strtemp.format(text(description = %s\n), (wchar_t*) e.description()); str+=\n; str+=strtemp; messagebox(str); }
五,执行语句
try { ptrconn->execute(select * from dbo.login,null,adcmdtext); ptrrecord->movefirst();//加了这句这会就可以取出数据了,可能是上一个父亲指针已经把其移动到最后了 int i= ptrrecord->recordcount;//如果把游标从动态到记录集,那么这个参数就有用 while(ptrrecord->endoffile==variant_false) { _variant_t va; _variant_t str; va.vt=vt_i4; va.lval=0; str = ptrrecord->fields->getitem(va)->value; cstring strgetid((wchar_t*)(_bstr_t)str); str = ptrrecord->fields->getitem(short(1))->value; cstring strgetserect((wchar_t*)(_bstr_t)str); strgetid.trimright(); strgetserect.trimright(); right=ptrrecord->fields->getitem(short(2))->value.intval; if(strgetid==strid&&strgetserect==strserect) { isadmin=true; } ptrrecord->movenext(); } } catch(_com_error &e) { cstring str; cstring strtemp; str.format(text(error:\n)); strtemp.format(text(code = %08lx\n), e.error()); str+=\n; str+=strtemp; strtemp.format(text(meaning = %s\n), e.errormessage()); str+=\n; str+=strtemp; strtemp.format(text(source = %s\n), (wchar_t*) e.source()); str+=\n; str+=strtemp; strtemp.format(text(description = %s\n), (wchar_t*) e.description()); str+=\n; str+=strtemp; afxmessagebox(str); }
connection::execute来执行sql语句,如果有返回结果的,那么就返回recordset,如果无需返回的就可以直接执行取出查询结果是
ptrrecord->fields->getitem(va)->value;
ptrrecord->fields->getitem(va)->name;
va是
_variant_t va;va.vt=vt_i4;va.lval=0;
也可以这样
ptrrecord->fields->getitem(short(0))->name;
返回结果是
while(ptrrecord->endoffile==variant_false)
{
取出结果集
ptrrecord->movenext();
}
有些时候,如果你的结果集已经被全部取出来的时候,也就是你的结果集已经到了非variant_false
这时候你就需要重新创建实例,然后打开,ptrrecord.createinstance(__uuidof(recordset));//有些时候如果记录级背使用过了,可能需要重新创建实例,然后再打开 ptrrecord->open(select * from dbo.staffinfo,///为什么把这里的数据库改成dbo.staffrecord时候,列表框就显示全为空 ptrconn.getinterfaceptr(), adopenkeyset, //注意在vb说明文档时候,首字母a是大写的,在c++中应该小写 adlockbatchoptimistic, adcmdtext);
这样你才能重新使用,不然好像movefirst,么有用啊
另外如果你结果集在某处调用了close,那么你再次使用的时候一定要执行上面这段语句,否则会报说记录集已经关闭了,wufa使用
六:command对象的使用
try
{
// create connection object (1.5 version)
conn1.createinstance( __uuidof( connection ) );
conn1->connectionstring = bstrconnect;
conn1->open( bstrempty, bstrempty, bstrempty, -1 );
// create command object
cmd1.createinstance( __uuidof( command ) );
cmd1->activeconnection = conn1;
cmd1->commandtext = _bstr_t(select * from mytable where age
然后执行就可以了cmd1->execute就ok了
结束:
一些释放操作,置空操作
