VC程序与数据库连接,程序写好后,为了使程序正确使用,必须手工到"控制面板"
->ODBC中进行数据库的连接设置.能否在VC程序中写代码,让程序自动完成此任务(可以的话,原程序该怎么写),或者是否还有其它的方法解决.

解决方案 »

  1.   

    http://expert.csdn.net/Expert/topic/2468/2468019.xml?temp=5.945987E-02
      

  2.   

    当然能,不能小看VC哦。
    例子方面很多拉。
    http://www.vckbase.com/code也是一个学习的好地方。
      

  3.   

    SQLConfigDataSource(NULL,ODBC_USER_DSN,"Microsoft Access Driver (*.mdb)\0","DSN=student\0DBQ=student.mdb");
    DSN=student为注册名称
    0DBQ=student.mdb为数据库的名称,并且在同一目录下
      

  4.   

    不同的数据库,参数会有区别,你试试:
    #include <odbcinst.h>
    #pragma comment(lib,"odbccp32.lib")void CDSNDlg::OnButtonDsn() 
    {
    char strDSN[]="MyDsn";
    char strDB[]="dbmain";
    char strPCName[]="192.168.0.1";
    unsigned char szAttributes[200];
    wsprintf((char*)szAttributes,
    "SQL Server\0",
    "DSN=%s\0"
    "DATABASE=%s\0"
    "SERVER=%s\0",
    strDSN,
    strDB,
    strPCName);
    if(!SQLConfigDataSource(
    NULL, 
    ODBC_ADD_SYS_DSN,
    "SQL Server",
    (LPCSTR)szAttributes))
    {
    DWORD error = GetLastError(); AfxMessageBox("配置SQL Server2000的ODBC数据源失败");
    return;
    }
    }void CDSNDlg::OnButtonMdb() 
    {
    if( 
    !SQLConfigDataSource(
    NULL,
    ODBC_ADD_DSN,
    "Microsoft Access Driver (*.mdb)",
    "DSN=CTI\0"
    "Description=Data source created by Wangjin's program\0"
    "DBQ=C:\\aaa.mdb\0"
    ) )
    {
    AfxMessageBox("Error");
    }
    }
      

  5.   

    odbcinst.h是什么头文件,我只用afxdb.h
      

  6.   

    用ADO直接连接SQL服务器的数据库。