locked
SQL Anywhere 12 and C++ (Connection) RRS feed

  • Question

  • How do I connect my application to a SQL Anywhere 12 database using C++?

    Here's my code below:

    class DatabaseConnection
    {
    public:
    	void Connect ();
    	void SQLCommand (string, string, string, string, string, string, string);
    	void Close ();
    
    	string DSN; // Database name and location. For example, "C:\MyDatabase.accdb"
    	string UID; // The user ID for the database.
    	string Pwd; // Password
    private:
    	SQLHENV hEnv; // ODBC Environment handle.
    	SQLHDBC hDbc; // ODBC Connection handle.
    	HSTMT hStmt; // ODBC Statement handle.
    	SQLRETURN rc;
    	string command;
    	string temp;
    };
    void DatabaseConnection::Connect ()
    {
    	rc = SQLAllocHandle (SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv); // Allocate ODBC environment handle
    	if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
    	{
    		//"UID=DBA;PWD=***;DBF=C:\Aklat_1.0_DB.db"
    		cout << "ODBC Environment handle allocated.<br>" << endl;
    		SQLSetEnvAttr (hEnv, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0); // Set the ODBC version
    		rc = SQLAllocHandle (SQL_HANDLE_DBC, hEnv, &hDbc); // Allocate ODBC Connection handle
    		if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
    		{
    			cout << "ODBC Connection handle allocated.<br>" << endl;
    
    			rc = SQLConnect (hDbc, (SQLWCHAR *) DSN.c_str (), SQL_NTS, (SQLWCHAR *) UID.c_str (), SQL_NTS, (SQLWCHAR *) Pwd.c_str (), SQL_NTS);
    			if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
    			{
    				cout << "Connecting using string: " << "Datasource = " << DSN << ";"<< " User ID = " << UID << ";" << " Password = " << Pwd << ";"<< "<br>" << endl;
    				cout << "Connection successful!<br>" << endl;
    			}
    			else 
    			{
    				cout << "Connecting using string: " << "Datasource = " << DSN << ";"<< " User ID = " << UID << ";" << " Password = " << Pwd << ";"<< "<br>" << endl; 
    				cout << "Connection failed!<br>" << endl;
    			}
    		}
    		else
    		{
    			cout << "ODBC Connection handle allocation failed!<br>" << endl;
    		}
    	}
    	else
    	{
    		cout << "ODBC Environment allocation failed!<br>" << endl;
    	}
    }
    void DatabaseConnection::SQLCommand (string TheIDNumber, string TheName, string TheOccupation, string TheCourseAndSection, string TheEmail, string ThePassword, string TheSex)
    {
    	temp = "INSERT INTO Borrower VALUES ";
    	command = temp + "(" + "'" + TheIDNumber + "'" + ", " + "'" + TheName + "'" + ", " + "'" + TheOccupation + "'" + ", " + "'" + TheCourseAndSection + "'"  + ", " + "'" + TheEmail + "'" + ", " + "'" + ThePassword + "'" + ", " + "'" + TheSex + "'" + ")";
    	rc = SQLExecDirect (hStmt, (SQLWCHAR *) command.c_str (), SQL_NTS);
    	if (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
    	{
    		cout << "SQL statement: " << command << ";<br>" << endl;
    		cout << "Database was updated successfully!<br>" << endl;
    	}
    	else
    	{
    		cout << "SQL statement: " << command << ";<br>" << endl;
    		cout << "Database was not updated.<br>" << endl;
    	}
    }
    void DatabaseConnection::Close ()
    {
    	SQLDisconnect (hDbc);
    	SQLFreeHandle (SQL_HANDLE_DBC, hDbc);
    	SQLFreeHandle (SQL_HANDLE_ENV, hEnv);
        SQLFreeHandle (SQL_HANDLE_STMT, hStmt);
    }

    void ConnectToDB ()
    {
    	DatabaseConnection DB;
    	DB.DSN = "MyDatasourceName";
    	DB.UID = "DBA";
    	DB.Pwd = "sql";
    	DB.Connect ();
    	DB.SQLCommand (IDNumber, Name, Occupation, CourseAndSection, Email, Password, Sex);
    	DB.Close ();
    }

    The output is "Connection Failed!".  I would really be happy if someone answers my question. I've been searching for answers but I got nothing. Please respond to my question. Thank you very much.

    • Moved by Jesse Jiang Thursday, August 23, 2012 2:03 AM (From:Visual C++ General)
    Tuesday, August 21, 2012 1:21 PM

Answers