如何用VC+ADO+SQLSERVER+STORE PROCDURE做一个DLL(用MFC向导生成的非扩展DLL)文件
请指导!!!我现在VC+ADO在做一个DLL(用MFC向导生成的非扩展DLL)文件给别人调用,连接一个sqlserver,并且执行存储过程.
但我先做了一个EXE文件(奇怪,#import "....\ msado15.dll"只能放在当前的CPP文件中,不能放在stdafx.h中),可以正常运.
-------
我将EXE文件改做成的DLL文件就报错,但dll报错.
"c:\program files\microsoft visual studio\myprojects\dectid\debug\msado15.tlh(275) : error C2011: 'EditModeEnum' : 'enum' type redefinition
c:\program files\microsoft visual studio\myprojects\dectid\debug\msado15.tlh(283) : error C2011: 'RecordStatusEnum' : 'enum' type redefinition
C:\Program Files\Microsoft Visual Studio\MyProjects\dectid\dectid.cpp(70) : error C2065: 'adBSTR' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\dectid\dectid.cpp(70) : error C2065: 'adParamInput' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\dectid\dectid.cpp(75) : error C2065: 'adInteger' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\dectid\dectid.cpp(75) : error C2065: 'adParamOutput' : undeclared identifier"
代码如下:
////////////////////////////////////////////////////////
// dectid.h : main header file for the DECTID DLL/////////////#if !defined(AFX_DECTID_H__8D64C27A_1B64_11D8_91E2_006097BC04CD__INCLUDED_)
#define AFX_DECTID_H__8D64C27A_1B64_11D8_91E2_006097BC04CD__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000#ifndef __AFXWIN_H__
#error include 'stdafx.h' before including this file for PCH
#endif
#include "resource.h"
extern "C"  void PASCAL EXPORT InitSQLServer(CString server,CString db,CString                         UserName,CString Pwd);
extern "C" CString  PASCAL EXPORT spgetbaseid(CString category,CString& bsemc1,CString& fpn1,CString& rpn1,CString& e1,CString& arc1,CString& stat1 );
extern "C"  CString PASCAL EXPORT spgethandsetid(CString category,CString& hsemc1,CString& psn1,CString& put1,CString& stat2); extern "C"  void  PASCAL EXPORT ExitDB();
class CDectidApp : public CWinApp
{
public:
CDectidApp();
   DECLARE_MESSAGE_MAP()
};
#endif // !defined(AFX_DECTID_H__8D64C27A_1B64_11D8_91E2_006097BC04CD__INCLUDED_)//////////////////////////////////////////////////////////////////////////////
////////dectid.cpp : Defines the initialization routines for the DLL.///////#include "stdafx.h"
#include "dectid.h"
//#include "comdef.h" 
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename( "EOF", "adoEOF" )
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif_ConnectionPtr  Conn1;
_CommandPtr cmd,cmd2;BEGIN_MESSAGE_MAP(CDectidApp, CWinApp)
//{{AFX_MSG_MAP(CDectidApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
//    DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CDectidApp theApp; extern "C"  void  PASCAL EXPORT ExitDB()
{  AFX_MANAGE_STATE(AfxGetStaticModuleState());
if(Conn1!=NULL)
{
Conn1->Close();//close
Conn1->Release();//release

}
}
 extern "C" CString  PASCAL EXPORT spgetbaseid(CString category,CString& bsemc1,CString& fpn1,CString& rpn1,CString& e1,CString& arc1,CString& stat1 )
{// CString bsemc1,fpn1,rpn1,e1,arc1,stat1 ;
 AFX_MANAGE_STATE(AfxGetStaticModuleState());
cmd.CreateInstance(__uuidof(Command));
_ParameterPtr Param1;
Param1.CreateInstance(__uuidof(Parameter));
Param1=cmd->CreateParameter(_bstr_t("category"),adBSTR,adParamInput,15,_bstr_t(category));
cmd->Parameters->Append(Param1);

_ParameterPtr Param2;
Param2.CreateInstance(__uuidof(Parameter));
Param2=cmd->CreateParameter(_bstr_t("bsemc"),adInteger,adParamOutput,4);
cmd->Parameters->Append(Param2);
_ParameterPtr Param3;
Param3.CreateInstance(__uuidof(Parameter));
Param3=cmd->CreateParameter(_bstr_t("fpn"),adInteger,adParamOutput,4);
cmd->Parameters->Append(Param3);
_ParameterPtr Param4;
Param4.CreateInstance(__uuidof(Parameter));
Param4=cmd->CreateParameter(_bstr_t("rpn"),adInteger,adParamOutput,4);
cmd->Parameters->Append(Param4);
_ParameterPtr Param5;
Param5.CreateInstance(__uuidof(Parameter));
Param5=cmd->CreateParameter(_bstr_t("e"),adInteger,adParamOutput,4);
cmd->Parameters->Append(Param5);
_ParameterPtr Param6;
Param6.CreateInstance(__uuidof(Parameter));
Param6=cmd->CreateParameter(_bstr_t("arc"),adInteger,adParamOutput,4);
cmd->Parameters->Append(Param6);
_ParameterPtr Param7;
Param7.CreateInstance(__uuidof(Parameter));
Param7=cmd->CreateParameter(_bstr_t("returnstatus"),adInteger,adParamOutput,4);
cmd->Parameters->Append(Param7);
cmd->ActiveConnection =Conn1;
cmd->CommandText="sp_getbaseid";
cmd->CommandType=adCmdStoredProc;
    //execute result
try{
cmd->Execute(NULL, NULL, adCmdStoredProc);
}
catch(...)
{AfxMessageBox("cmd error");}
       bsemc1=(char*)_bstr_t(Param2->Value);
fpn1=(char*)_bstr_t(Param3->Value);
rpn1=(char*)_bstr_t(Param4->Value);
e1=(char*)_bstr_t(Param5->Value);
arc1=(char*)_bstr_t(Param6->Value);
stat1=(char*)_bstr_t(Param7->Value); return (char*)_bstr_t(Param7->Value);
}
extern "C"  void  PASCAL EXPORT InitSQLServer(CString server,CString db,CString UserName,CString Pwd){
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
Conn1.CreateInstance(__uuidof(Connection));

CString strCn;
strCn.Empty();

strCn ="Provider=SQLOLEDB.1;Password="+Pwd+"Persist Security Info=True;User ID="+UserName+";Initial Catalog="+db+";Data Source="+server ;
_variant_t bcnstr=_variant_t(strCn);
_variant_t bunstr=_variant_t(_bstr_t(UserName));
_variant_t bpwdstr=_variant_t(_bstr_t(Pwd));

try{
Conn1->Open(_bstr_t(bcnstr),_bstr_t(bunstr),
_bstr_t(bpwdstr),-1);}
catch(...)
{AfxMessageBox("ado error");
}

}
  
 extern "C"  CString  PASCAL EXPORT spgethandsetid(CString category,CString& hsemc1,CString& psn1,CString& put1,CString& stat2)
{

 AFX_MANAGE_STATE(AfxGetStaticModuleState());
cmd2.CreateInstance(__uuidof(Command));
_ParameterPtr Paramh1;
Paramh1.CreateInstance(__uuidof(Parameter));
Paramh1=cmd2->CreateParameter(_bstr_t("category"),adBSTR,adParamInput,15,_bstr_t(category));
cmd2->Parameters->Append(Paramh1);
_ParameterPtr Paramh2;
Paramh2.CreateInstance(__uuidof(Parameter));
Paramh2=cmd2->CreateParameter(_bstr_t("hsemc"),adInteger,adParamOutput,4);
cmd2->Parameters->Append(Paramh2);
_ParameterPtr Paramh3;
Paramh3.CreateInstance(__uuidof(Parameter));
Paramh3=cmd2->CreateParameter(_bstr_t("psn"),adInteger,adParamOutput,4);
cmd2->Parameters->Append(Paramh3);
_ParameterPtr Paramh4;
Paramh4.CreateInstance(__uuidof(Parameter));
Paramh4=cmd2->CreateParameter(_bstr_t("put"),adInteger,adParamOutput,4);
cmd2->Parameters->Append(Paramh4);
_ParameterPtr Paramh5;
Paramh5.CreateInstance(__uuidof(Parameter));
Paramh5=cmd2->CreateParameter(_bstr_t("returnstatus"),adInteger,adParamOutput,4);
cmd2->Parameters->Append(Paramh5);
    cmd2->ActiveConnection =Conn1;
cmd2->CommandText="sp_gethandsetid";
cmd2->CommandType=adCmdStoredProc;//adCmdStoredProc
    //excute result
try{
cmd2->Execute(NULL, NULL, adCmdStoredProc);
}
catch(...)
{AfxMessageBox("cmd error");}//(NULL, NULL, adCmdStoredProc);
// MessageBox(_bstr_t(Param2->Value));
    hsemc1=(char*)_bstr_t(Paramh2->Value);
psn1=(char*)_bstr_t(Paramh3->Value);
put1=(char*)_bstr_t(Paramh4->Value);
stat2=(char*)_bstr_t(Paramh5->Value);
return (char*)_bstr_t(Paramh5->Value);
}

解决方案 »

  1.   

    这个错误是因为你可能在头文件包含中多加了AFXDAO。H,这个文件和ADO冲突,在stdafx.h中找到相应的行注释到即可解决
      

  2.   

    如果你在一个工程里同时使用DAO和ADO,那么会由于定义了相同的变量或者枚举类型而无法编译通过。你可以为ADO重定义名字空间。比如:
    #import "C:\Program Files\Common Files\System\ado\msado15.dll" rename_namespace("ADO") rename( "EOF", "adoEOF" )在使用的时候戴上名字空间就可以了:
    ADO::ConnectionPtr m_pCnt;
      

  3.   

    http://expert.csdn.net/Expert/topic/2768/2768589.xml?temp=.9567377