下面是创建ODBC数据源的例子,里面的数据是写死的,我想将它们变成输入参数。那个szAttributes该如何拼装呢?谢谢!

RETCODE retcode;
char* szDriver="SQL Server";//数据库驱动名
char* szAttributes="DSN=MyFirstDsn\0"//指定数据源名称
   "DESCRIPTION=测试如何通过程序创建数据源\0"//指定描述信息
   "SERVER=HESL\0"//指定服务器    
   "DATABASE=MyTest\0";//指定数据库名称
retcode=SQLConfigDataSource(NULL,ODBC_ADD_SYS_DSN,szDriver,szAttributes);
if(retcode)
{
AfxMessageBox("数据源创建成功!");
}
else
{
AfxMessageBox("数据源创建失败!");
}
//创建数据源。参数的含义:
//strDataSName——[IN]数据源名;strDescription——[IN]描述信息
//strDriverName——[IN]数据库驱动名;strServerName——[IN]服务器名;strDbName——[IN]数据库名
BOOL COdbcDataSOper::createDataS(CString strDataSName,CString strDescription,CString strDriverName,CString strServerName,CString strDbName)
{ RETCODE retcode;
char* szDriver=(LPCTSTR)strDriverName;//数据库驱动名 char* szAttributes=????//这个地方如何拼装 retcode=SQLConfigDataSource(NULL,ODBC_ADD_SYS_DSN,szDriver,szAttributes);
if(retcode)
{
AfxMessageBox("数据源创建成功!");
}
else
{
AfxMessageBox("数据源创建失败!");
} return retcode;
}

解决方案 »

  1.   

    用CString吧,否则用sprintf来做,自己要管理内存
      

  2.   

    我把代码贴出来了,哪位大侠有空帮我测试一下好吗?我这里总是创建失败,郁闷!谢谢了!
    //创建数据源。参数的含义:
    //strDataSName——[IN]数据源名;strDescription——[IN]描述信息
    //strDriverName——[IN]数据库驱动名;strServerName——[IN]服务器名;strDbName——[IN]数据库名
    BOOL COdbcDataSOper::createDataS(CString strDataSName,CString strDescription,CString strDriverName,CString strServerName,CString strDbName)
    {
    strDataSName.TrimLeft();
    strDataSName.TrimRight();
    strDescription.TrimLeft();
    strDescription.TrimRight();
    strDriverName.TrimLeft();
    strDriverName.TrimRight();
    strServerName.TrimLeft();
    strServerName.TrimRight();
    strDbName.TrimLeft();
    strDbName.TrimRight(); int lenOfDataSName=strDataSName.GetLength()+5;
    char* pDataSName=new char[lenOfDataSName];
    ASSERT(pDataSName);
    memset(pDataSName,'\0',lenOfDataSName);
    strcpy(pDataSName,"DSN=");
    strcat(pDataSName,(LPCTSTR)strDataSName);

    int lenOfDescription=strDescription.GetLength()+13;
    char* pDescription=new char[lenOfDescription];
    ASSERT(pDescription);
    memset(pDescription,'\0',lenOfDescription);
    strcpy(pDescription,"DESCRIPTION=");
    strcat(pDescription,(LPCTSTR)strDescription); int lenOfServerName=strServerName.GetLength()+8;
    char* pServerName=new char[lenOfServerName];
    ASSERT(pServerName);
    memset(pServerName,'\0',lenOfServerName);
    strcpy(pServerName,"SERVER=");
    strcat(pServerName,(LPCTSTR)strServerName);

    int lenOfDbName=strDbName.GetLength()+10;
    char* pDbName=new char[lenOfDbName];
    ASSERT(pDbName);
    memset(pDbName,'\0',lenOfDbName);
    strcpy(pDbName,"DATABASE=");
    strcat(pDbName,(LPCTSTR)strDbName);

    char* szAttributes[4];
    szAttributes[0]=pDataSName;
    szAttributes[1]=pDescription;
    szAttributes[2]=pServerName;
    szAttributes[3]=pDbName; retcode=SQLConfigDataSource(NULL,ODBC_ADD_SYS_DSN,szDriver,szAttributes[0]);
    if(retcode)
    {
    AfxMessageBox("数据源创建成功!");
    }
    else
    {
    AfxMessageBox("数据源创建失败!");
    } return retcode;
    }
    void CMyDataSTestDlg::OnAlltest() 
    {
    //测试创建ODBC数据源
    BOOL isOkOfCreateDataS=m_OdbcDataSOper.createDataS("MyDataS","测试!","SQL Server","aasfd","Mydd");
    }
      

  3.   

    用字符数组这段:
    char* szAttributes[4];
    szAttributes[0]=pDataSName;
    szAttributes[1]=pDescription;
    szAttributes[2]=pServerName;
    szAttributes[3]=pDbName;
    改为:
    char buf[255];
    int nLen = 0;
    sprintf(buf, "%s", pDataSName);
    nLen += strlen(pDataSName) + 1;
    sprintf(&buf[nLen],"%s", pDescription);
    nLen += strlen(pDescription) + 1;
    sprintf(&buf[nLen],"%s", pServerName);
    ...试试?
      

  4.   

    char buf[255];
    后面应该有
    memset(buf, 0, 255);