[cstring]sql 如何读取值为:?? 到CString

2018-08-17 16:37  阅读 491 次

sql 如何读取值为:?? 到CString

问题补充:sql 如何读取值为:?? 到CString
●1、在stdafx.h文件最后(即#endif // _AFX_NO_AFXCMN_SUPPORT下面)添加: #import "c:program filescommon filessystemadomsado15.dll" no_namespace rename("EOF","adoEOF") 2、初始化COM: AfxOleInit();//这行代码要放在功能执行前,如果是基于对话框建立的程序,那就放在第一个对话框类的OnInitDialog()函数的return TRUE;前 3、在用到数据库的地方: _ConnectionPtr m_pConnection;///声明数据库连接变量 _RecordsetPtr m_pRecordset;///声明数据库集合变量 CString strCn; strCn.Empty(); (1)连接数据库 HRESULT hr; try { _variant_t RecordsAffected; hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象 if(SUCCEEDED(hr)) { hr = m_pConnection->Open("DSN=test;UID=;PWD=;","","",adModeUnknown);///连接数据库 } } catch( _com_error e)///捕捉异常 { CString errormessage; errormessage.Format("连接数据库失败!rn错误信息:%s",e.ErrorMessage()); AfxMessageBox(errormessage);///显示错误信息 } (2)通过SQL读数据 CString sql; try { m_pRecordset.CreateInstance("ADODB.Recordset"); m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); } catch(_com_error e)///捕捉异常 { CString errorMessage = e.ErrorMessage(); AfxMessageBox("读取数据时出错:"+sql+errorMessage);///显示错误信息 } (3)通过sql语句添加、修改、删除记录 _variant_t RecordsAffected; try { m_pConnection->Execute((_bstr_t)Sql,&RecordsAffected,adCmdText); } catch(_com_error *e) { AfxMessageBox(e->ErrorMessage()); }

c++ string类二进制文件怎么读

问题补充:这个代码究竟错在哪,怎么改,一定要用string类#include<iostream>#include<string>#include<fstream>using namespace std;int main(){ string a,b; cin>>a; ofstream ofile; ofile.open("d:\\1.dat"); ofile.write((char*)&a,sizeof(a)); ofile.close(); ifstream ifile; ifile.open("d:\\1.dat"); ifile.read((char*)&b,sizeof(b)); ifile.close(); cout<<b<<endl;}
●ofile.write((char*)&a,sizeof(a)); 改成 ofile.write((char*) a.c_str(), a.length()); 或者ofile << a;ifile.read((char*)&b,sizeof(b)); 改成ifile >> b;试试,你要理解 read write函数的意思,不要乱写代码

本文地址:https://www.ehuixue.com/12406.html
关注我们:微信公众号:扫描二维码慧学在线的微信号,公众号:********
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!

发表评论


表情