用C/C++开发数据库应用程序的编译器选择问题 想在Windows平台下用C/C++开发一个无界面的数据库应用程序,除了用VC,大家说说看还有什么编译器比较合适?要求所写的程序必须可在VC外的其他C编译器下编译通过,如果我用VC下的Console程序写,有哪些需要注意的地方?对数据库的连接用什么方式比较合适(主要是Oracle和Sybase数据库)?(如果用ADO方式要求包含stdafx.h头文件,这样就不能独立于VC环境),谢谢了,分不够可以加。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不一定要包含stdafx.h,将import命令放到你想放的地方。 用ADO要求包含stdafx.h么?没这回事在自己的头文件中#import也一样 偶公司的服务器程序基本都是纯C++的就是用VC写 用VC的Console写的程序不能引用MFC的类吧,就是说写的Console程序肯定可以在其他C编译器下运行是吗? 1.用VC写的console可以引用MFC类2.用纯VC写的不一定能用其它编译通过,如以下程序:#include <windows.h>#include <stdio.h>#include <comdef.h>#import "d:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")void main(){ CoInitialize(NULL); _ConnectionPtr m_pConnection; HRESULT hr; hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象 if(SUCCEEDED(hr)) hr = m_pConnection->Open(L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\temp\\DBTEST\\db1.mdb;Persist Security Info=True",L"",L"",adModeUnknown);///连接数据库 else { printf("error\n"); return; } if(!SUCCEEDED(hr)) { printf("error\n"); return; } _variant_t RecordsAffected; _RecordsetPtr m_pRecordset = m_pConnection->Execute(L"SELECT COUNT(*) FROM users",&RecordsAffected,adCmdText); _variant_t vIndex = (long)0; _variant_t vCount = m_pRecordset->GetCollect(vIndex);///取得第一个字段的值放入vCount变量 m_pRecordset->Close();///关闭记录集 printf("共有%d条记录\n",vCount.lVal); _CommandPtr m_pCommand; m_pCommand.CreateInstance("ADODB.Command"); _variant_t vNULL; vNULL.vt = VT_ERROR; vNULL.scode = DISP_E_PARAMNOTFOUND;///定义为无参数 m_pCommand->ActiveConnection = m_pConnection;///非常关键的一句,将建立的连接赋值给它 m_pCommand->CommandText = "SELECT id,username FROM users";///命令字串 m_pRecordset = m_pCommand->Execute(&vNULL,&vNULL,adCmdText);///执行命令,取得记录集 while(!m_pRecordset->adoEOF) { vIndex = (long)0; _variant_t id = m_pRecordset->GetCollect(vIndex);///取得第一个字段的值放入vCount变量 vIndex = (long)1; _variant_t vName = m_pRecordset->GetCollect(vIndex);///取得第一个字段的值放入vCount变量 int nLen = wcslen(vName.bstrVal)+1; char *buf = new char[2*nLen]; WideCharToMultiByte(CP_ACP, 0, vName.bstrVal, nLen, buf, 2*nLen, NULL, NULL); printf("id=%d\tname=%s\n",id.iVal,buf); delete buf; m_pRecordset->MoveNext(); } m_pRecordset->Close();///关闭记录集 if(m_pConnection->State) m_pConnection->Close(); ///如果已经打开了连接则关闭它}用VC没问题,用BCB或GCC就不行 要用其他编译器那么不要用MFC,ado什么的。直接调用ODBC API可能是通用性最好的 lcstudio(铁板牛牛),能给个例子吗?不晓得这种方式怎么写。thx:) 关于view类和对话框类通信的问题 CSplitterWnd能不能动态的指定为指定的行数? vista 64位下右键菜单取不到选中的文件名,32位中正常工作 VC调用DLL过程中出现的问题 vc调用webservice问题 自启动程序的问题,帮顶有分,在线 求助帮忙 vc连接上ftp服务器后,怎么取得其文件夹和文件列表? :)有没有对等值线绘制与颜色填充算法感兴趣的朋友,快进来!100分大赠送 为什么敲回车后我的默认按钮没有反映? 进度条相关 关于代理服务器问题,如能帮助解决愿送500分!在线等!
没这回事
在自己的头文件中#import也一样
就是用VC写
2.用纯VC写的不一定能用其它编译通过,如以下程序:
#include <windows.h>
#include <stdio.h>
#include <comdef.h>
#import "d:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
void main()
{
CoInitialize(NULL); _ConnectionPtr m_pConnection;
HRESULT hr;
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
hr = m_pConnection->Open(L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\temp\\DBTEST\\db1.mdb;Persist Security Info=True",L"",L"",adModeUnknown);///连接数据库
else
{
printf("error\n");
return;
} if(!SUCCEEDED(hr))
{
printf("error\n");
return;
}
_variant_t RecordsAffected;
_RecordsetPtr m_pRecordset = m_pConnection->Execute(L"SELECT COUNT(*) FROM users",&RecordsAffected,adCmdText);
_variant_t vIndex = (long)0;
_variant_t vCount = m_pRecordset->GetCollect(vIndex);///取得第一个字段的值放入vCount变量
m_pRecordset->Close();///关闭记录集
printf("共有%d条记录\n",vCount.lVal); _CommandPtr m_pCommand;
m_pCommand.CreateInstance("ADODB.Command");
_variant_t vNULL;
vNULL.vt = VT_ERROR;
vNULL.scode = DISP_E_PARAMNOTFOUND;///定义为无参数
m_pCommand->ActiveConnection = m_pConnection;///非常关键的一句,将建立的连接赋值给它
m_pCommand->CommandText = "SELECT id,username FROM users";///命令字串
m_pRecordset = m_pCommand->Execute(&vNULL,&vNULL,adCmdText);///执行命令,取得记录集
while(!m_pRecordset->adoEOF)
{
vIndex = (long)0;
_variant_t id = m_pRecordset->GetCollect(vIndex);///取得第一个字段的值放入vCount变量
vIndex = (long)1;
_variant_t vName = m_pRecordset->GetCollect(vIndex);///取得第一个字段的值放入vCount变量
int nLen = wcslen(vName.bstrVal)+1;
char *buf = new char[2*nLen];
WideCharToMultiByte(CP_ACP, 0, vName.bstrVal, nLen, buf, 2*nLen, NULL, NULL);
printf("id=%d\tname=%s\n",id.iVal,buf);
delete buf; m_pRecordset->MoveNext(); }
m_pRecordset->Close();///关闭记录集 if(m_pConnection->State)
m_pConnection->Close(); ///如果已经打开了连接则关闭它}用VC没问题,用BCB或GCC就不行