现在来CSDN社区的时间比较多了,我从我自己得分的问题里选出了一部分自认为比较有代表性的整理成一个精华版,也算自己对VC/MFC版尽一点绵薄之力吧,虽然问题本身也许没有什么代表性,或者对一些新手不太有帮助,但是我自认为涵盖了一些比较重要的话题,希望对大家有所帮助。
Q 怎样在vc中导出Excel表格?
A
导出数据:
SQL可以实现:
SELECT * INTO Material IN 'C:/DB' 'Excel 8.0' FROM tblMaterial
说明:
into 后面的 material 是XLS文件中要导入的表名
in后面的 'C:/DB' 就是你的导的XLS文件。
excel 8.0是要导出的数据库格式,把它换成 dBase IV 就可以导出 DBF 文件
tblmaterial 是MDB数据库中的要导出的表名
导入数据:
参考VB函数:
http://search.csdn.net/Expert/topic/828/828974.xml?temp=.6185572
Q ADO如何取得数据库中表的表名
A
_variant_t vFieldValue;
CString strFieldValue;
m_pRs=m_pConnection->OpenSchema(adSchemaTables);
while(VARIANT_FALSE==m_pRs->IsEOF)
{
strFieldValue=(char*)_bstr_t(m_pRs->GetCollect("TABLE_TYPE"));
if(!strcmp(strFieldValue.GetBuffer(0),"TABLE")||!strcmp(strFieldValue.GetBuffer(0),"table"))
{
strFieldValue.ReleaseBuffer();
strFieldValue=(char*)_bstr_t(m_pRs->GetCollect("TABLE_NAME"));
m_ctlList.AddString(strFieldValue);
}
m_pRs->MoveNext();
}
m_pRs->Close();
Q 在stdafx中加入
#import "C:/Program Files/Common Files/System/ADO/msado15.dll" no_namespace rename("EOF","adoEOF")后总是编译通不过,说是有EditModeEnum、RecordStatusEnum、ParameterDirectionEnum、LockTypeEnum、DataTypeEnum、FieldAttributeEnum重定义!
A
改成
#pragma warning(disable:4146)
#import "C:/Program Files/Common Files/System/ADO/msado15.dll" named_guids rename("EOF","adoEOF"), rename("BOF","adoBOF")
#pragma warning(default:4146)
using namespace ADODB; 后错误都没有了
Q 怎么取得一个表的数据段信息(数据类型、长度、名称等)
A
#include
#define PAUSE ?printf("/npress any key to exit"); getch();
#define _WIN32_DCOM
#pragma warning(push)
#pragma warning(disable:4146)
#import "e:/program files/common files/system/ado/MSADO15.DLL" no_namespace rename("EOF", "EndOfFile")
#pragma warning(pop)
int main(int argc, char* argv[])
{
_ConnectionPtr m_pConn;
_RecordsetPtr m_pRs;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
_bstr_t bstrConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;"
"Data Source=F://lylong//db1.mdb;Persist Security Info=False";
try{
m_pConn.CreateInstance( __uuidof(Connection) );
m_pRs.CreateInstance( __uuidof(Recordset) );
m_pConn->Open( bstrConnStr, "", "", adConnectUnspecified );
m_pRs->Open( "table1",
m_pConn.GetInterfacePtr(), adOpenForwardOnly, adLockOptimistic, adCmdTable);
long lColumn = m_pRs->Fields->Count;
printf("%-12s%-10s%-10s/n", "字段名", "长度", "类型");
for( long i=0; i{
printf("%-12s%-10d%-10d/n", (char*)m_pRs->Fields->Item[i]->Name,
m_pRs->Fields->Item[i]->DefinedSize,
m_pRs->Fields->Item[i]->Type);
}
m_pRs->Close();
m_pConn->Close();
}catch( _com_error e )
{
printf( "/n%s Error: %ld/r/n"
"%s/r/n"
"%s/r/n",
(char*)e.Source(),
e.Error(),
(char*)e.Description(),
(char*)e.ErrorMessage() );
m_pRs->Close();
m_pConn->Close();
}
CoUninitialize();
PAUSE;
return 0;
}
Also see this article
http://dev.csdn.net/article/25/25578.shtm
Q 如何读写SQL+ADO的datetime
A
写入时间值
COleDateTime oleDate = COleDateTime::GetCurrentTime();
_variant_t vtFld;
vtFld.vt = VT_DATE;
vtFld.date = oleDate;
pRecordSet4->PutCollect("date", vtFld);
读时间值
_bstr_t TheValue=m_pset->Fields->GetItem("出生年月")->Value;
temp=(char *)TheValue;
COleVariant vtime(temp);
vtime.ChangeType(VT_DATE);
COleDateTime time4=vtime;//读出的是COleDateTime
Q 如何判断access数据库中是否存在某个表
A
STDMETHODIMP CADOTier::get_IsExistTable(BSTR bsTable, long lType, VARIANT_BOOL *pVal)
{
ADODB::_RecordsetPtr pRstSchema = NULL;
pRstSchema = m_connection->OpenSchema(ADODB::adSchemaTables);
_bstr_t bsTableName(bsTable);
_bstr_t table_name("");
_bstr_t table_type("");
char *pTemp1=NULL,*pTemp2=NULL;
pTemp1 = _com_util::ConvertBSTRToString(bsTableName);
pTemp1 = strlwr(pTemp1);
VARIANT_BOOL b=FALSE;
while(!(pRstSchema->adoEOF))
{
table_name = pRstSchema->Fields->
GetItem("TABLE_NAME")->Value;
pTemp2 = _com_util::ConvertBSTRToString(table_name);
pTemp2 = strlwr(pTemp2);
table_type = pRstSchema->Fields->
GetItem("TABLE_TYPE")->Value;
if (lType == 1) //view type
{
if (table_type == _bstr_t("VIEW"))
{
if (strcmp(pTemp1,pTemp2)==0)
b = TRUE;
}
}
if (lType == 0) //table type
{
if (table_type == _bstr_t("TABLE"))
{
if (strcmp(pTemp1,pTemp2)==0)
b = TRUE;
}
}
pRstSchema->MoveNext();
}
// Clean up objects before exit.
if (pRstSchema)
if (pRstSchema->State == ADODB::adStateOpen)
pRstSchema->Close();
*pVal = b;
return S_OK;
}
Q 用代码实现自动注册一个工程目录下的access数据库的ODBC数据源
A
CString sPath;
GetModuleFileName(NULL,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);//得到程序的路径
sPath.ReleaseBuffer ();
int nPos;
nPos=sPath.ReverseFind('//');
sPath=sPath.Left(nPos);
nPos=sPath.ReverseFind('//');
sPath=sPath.Left(nPos);
CString lpszFile=sPath+"//lhwy.mdb"; //得到程序目录下的数据库的完整路径
char* szDesc;
int mlen;
szDesc=new char[256];
sprintf(szDesc,"DSN=%s? DESCRIPTION=TOC support source? DBQ=%s? FIL=MicrosoftAccess? DEFAULTDIR=%s??","lhwy",lpszFile,sPath); //形成一个SQLConfigDataSource函数参数串结构
mlen = strlen(szDesc);
for (int i=0; i
{
if(szDesc[i]=='?')
szDesc[i] = '/0'; //替换上面串的?为/0,形成一个完整的串
}
if (FALSE == SQLConfigDataSource(NULL,ODBC_ADD_DSN,"Microsoft Access Driver (*.mdb)/0",(LPCSTR)szDesc))//注册数据库
AfxMessageBox("SQLConfigDataSource Failed");
Q SQL Server实现模糊查询
A
strSQL.Format("select * from 表名 where 列 like '%%%s%%'",keyword);