Connect to oracle server using CDatabase from MFC

659 Views Asked by At

I try to connect to a oracle database, where server is installed on my laptop and run on port 1521 and the name is "orcl". First, I connect using Java and jdbc using this code:

private static final String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
private static final String USERNAME = "cosmin";
private static final String PASSWORD = "123456";
private static Connection conn;
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);

And now I try to connect using MFC and using CDatabase class but I can't connect.

    CDatabase db;
    TRY{
        db.OpenEx(L"ODBC;DRIVER={MICROSOFT ODBC FOR ORACLE};DSN=orcl;SERVER=@localhost:1521:orcl;UID=cosmin; PWD=123456");

        db.Close();
    } CATCH(CDBException, e) {
        AfxMessageBox(L"Database error: " + e->m_strError);
    }

I receive the next error: Datasource name not found and no default driver specified. How can I resolve this problem?

Thanks!

1

There are 1 best solutions below

0
On

If you are going to us a data source name. Here is how to get to the tool.

And if you want to add the name in your program:

BOOL ret= SQLConfigDataSource(
    NULL,
    ODBC_ADD_DSN,
    (LPSTR) "Microsoft Access Driver (*.mdb)\0",
    (LPSTR) "DSN=MS Access Database\0"
    "Description=MS Access Database\0"
    );
if( ! ret )
{
    AfxMessageBox( _T("The 'Data Source Name' failed to install\nPlease call Tech Support\n800-123-4567") );
    return FALSE;
}

On the other hand, (I have not tried it), you should be able to use a DSN-less connection string. But that means you can't DSN=orcl; as there is no such DSN.