c++针对数据库,文件的操作总结(原始)
1.将文件保存到sqlserver数据库的相关操作: Update t1 set filedata = ‘.txt’ , filedata = ( select * from openrowset( bulk ‘E:\1.txt’, SINGLE_BLOB ) as data ) where resultId = 5 Select convert( varchar(8000), data ) from t1 where resultId = 2 注:fileType: varchar(20), filedata: varbinary(MAX) 2.将sqlserver里的文件数据读出来到文件: CFile outfile(...); // 参考MSDN LPSTR buffer = (LPSTR)GlobalLock( cdbvariant.m_pbinary->m_hData ); Outfile.Write(buffer,cdbvariant.m_pbinary->m_dwDataLength); GlobalLock(cdbvariant.m_pbinary->m_hData); Outfile.close(); 执行某个文件: shellExecute( NULL,NULL,_T(“12.doc”), NULL,_T(“E:\\12.doc”),NULL); 3.文件查找: CFileFind findfile; CString path = “D:\\”; If( path.Right(1) != “\\”) path = path + “\\*.bmp”; bool res = findfile.FindFile(path); While(res) { res = findfile.FindNextFile(); if(!findfile.IsDots()) { findfile.GetFileTitle(); .... } } 4.MFC显示图片 HBITMAP hp; hp = (HBITMAP)LoadImage(NULL,_T(findfile.GetFilePath()),IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LRCREATEDIBSECTION); m_pictureshow.setBitmap(hp); 5.得到当前的时间 CTime ct = CTime::GetCurrentTime(); CString nowTime = ct.Format(“%Y-%m-%d %H:%M:%S”); 6.连接数据库 #include<afxdb.h> CDatabase cdb; if(!cdb.OpenEx( Getconnect(),CDatabase::noodbcDialog)){ return;} CRecordSet crs(&cdb); try { if( !crs.open( CRecordSet::dynamic, _T(“select * from t”))) { return ; } }catch( CDBException ee ){} short count = crs.GetODBCFiledCount(); int row = 0; crs.MoveFirst(); CDBVariant cdbvariant; while( !crs.IsEOF() ) { int col =0; for( short index = 0; index < count; ++index) { crs.GetFileValue(index,cdbvariant); m_list.AddItem(row, ++col, ....); } ++row ; crs.MoveNext(); } crs.close(); cdb.close(); 7.打开word保存数据 Colevariant, CApplication, CDocuments, CDocument0, CSelection