我想动态配置一个Excel数据源,我做的是一个基于对话框的程序,我是这样做的:
#include <odbcinst.h>
BOOL CTikuDlg::OnInitDialog()
{
CString sPath;
GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer ();
int nPos;
nPos=sPath.ReverseFind ('\\');
sPath=sPath.Left (nPos);
CString lpszFile = sPath + "\\tiku.mdb"; char* szDesc;
int mlen;
szDesc=new char[256];
sprintf(szDesc,"DSN=%s? DESCRIPTION=TOC support source? DBQ=%s? FIL=MicrosoftAccess? DEFAULTDIR=%s?? ","not",lpszFile,sPath);
mlen = strlen(szDesc);
for (int i=0; i<mlen; i++)
{
if (szDesc[i] == '?')
szDesc[i] = '\0';
}
if (FALSE == SQLConfigDataSource(NULL,ODBC_ADD_DSN,"Microsoft Access Driver (*.mdb)\0",(LPCSTR)szDesc))
AfxMessageBox("Failed");
}
为什么这样做还不行呀,应该怎么做呀,高手请点。。谢谢
#include <odbcinst.h>
BOOL CTikuDlg::OnInitDialog()
{
CString sPath;
GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer ();
int nPos;
nPos=sPath.ReverseFind ('\\');
sPath=sPath.Left (nPos);
CString lpszFile = sPath + "\\tiku.mdb"; char* szDesc;
int mlen;
szDesc=new char[256];
sprintf(szDesc,"DSN=%s? DESCRIPTION=TOC support source? DBQ=%s? FIL=MicrosoftAccess? DEFAULTDIR=%s?? ","not",lpszFile,sPath);
mlen = strlen(szDesc);
for (int i=0; i<mlen; i++)
{
if (szDesc[i] == '?')
szDesc[i] = '\0';
}
if (FALSE == SQLConfigDataSource(NULL,ODBC_ADD_DSN,"Microsoft Access Driver (*.mdb)\0",(LPCSTR)szDesc))
AfxMessageBox("Failed");
}
为什么这样做还不行呀,应该怎么做呀,高手请点。。谢谢
{
m_pGridCtrl=NULL;
char* szDesc;
int mlen;
szDesc=new char[256];
TCHAR szPath[MAX_PATH];
::GetModuleFileName(
NULL, // handle to module
szPath, // file name of module
MAX_PATH-1 // size of buffer
);//获取进程映象文件的绝对路径 CString strPathFile=szPath;
CFile tempfile;
tempfile.Open(szPath,CFile::modeRead);
strPathFile.TrimRight(tempfile.GetFileName());
tempfile.Close();
CString strFullPath;//获取数据库文件的绝对路径
strFullPath=strPathFile+"matmis.mdb";
m_strDataBaseFilePathName=strFullPath;
//////////////////////////////////////////
// CFileException ex;
if(!(tempfile.Open(m_strDataBaseFilePathName,CFile::modeRead)))
{
TCHAR szError[1024];
//ex.GetErrorMessage(szError, 1024);
strcpy(szError,_T("无法打开数据库,请退出系统!"));
MessageBox(szError,_T("连接ODBC数据源错误"),MB_ICONSTOP);
//PostMessage(WM_CLOSE);
}
//////////////////////////////////////////
sprintf(szDesc,"DSN=%s* DESCRIPTION=物流信息系统数据库* DBQ=%s* FIL=MicrosoftAccess* DEFAULTDIR=%s**","物流信息系统数据库",(const char*)strFullPath,(const char*)strPathFile);
mlen = strlen(szDesc);
for (int i=0; i<mlen; i++)
{
if (szDesc[i] == '*')
szDesc[i] = '\0';
}
//创建ODBC数据源
if (FALSE == SQLConfigDataSource(NULL,ODBC_ADD_DSN,"Microsoft Access Driver (*.mdb)\0",(LPCSTR)szDesc))
AfxMessageBox("Failed");
delete []szDesc;
}
谢谢
louifox(兰陵笑笑生)