Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. convert CString to SQLCHAR*

convert CString to SQLCHAR*

Scheduled Pinned Locked Moved C / C++ / MFC
databasehelp
20 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A anna mathew

    SEE MY CODE BELOW: Ive got a dialog box where i enter Contact Details...... Ive To Insert IT Into Access Database........ //Take the values entered in edit box to CEdit * m_First.GetWindowText(Firstname); m_Last.GetWindowText(Lastname); m_Add.GetWindowText(Address); CString FullName = "INSERT INTO Contacts (FirstName, LastName, Address) VALUES ('" +Firstname+ "', '" +Lastname+"','" + Address+"'))"; executeSQL((SQLCHAR *) FullName); ERROR : CANNOT CONVERT CString to SQLCHAR*

    A Offline
    A Offline
    anna mathew
    wrote on last edited by
    #11

    Ive Edited My Code To: void CInsert::OnBnClickedOk() { OnOK(); SQLRETURN sr; SQLHSTMT hstmt; SQLCHAR SQL[] ="INSERT INTO Contacts (FirstName, LastName, Address) VALUES('?','?','?')"; //Take the values entered in edit box to CEdit * m_First.GetWindowText(Firstname); m_Last.GetWindowText(Lastname); m_Add.GetWindowText(Address); SQLINTEGER FirstNameLength = SQL_NTS; SQLINTEGER AddressLength = SQL_NTS; SQLINTEGER LastNameLength = SQL_NTS; SQLINTEGER FullLength = SQL_NTS; // Allocate a new statement handle sr = SQLAllocHandle(SQL_HANDLE_STMT, hDbConn, &hstmt); // Prepare statement if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in SQLAllocHandle in OnViewTestpreparedinsert"); sr = SQLPrepare(hstmt, SQL, SQL_NTS); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in SQLPrepare in OnViewTestpreparedinsert"); // Bind Parameters sr = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 10, 0, &Firstname, sizeof(Firstname), &FirstNameLength); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in Binding 2 in OnViewTestpreparedinsert"); sr = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 10, 0, &Lastname, sizeof(Lastname), &LastNameLength); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in Binding 3 in OnViewTestpreparedinsert"); sr = SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT,SQL_C_CHAR, SQL_CHAR, 10, 0, &Address, sizeof(Address), &AddressLength); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in Binding 3 in OnViewTestpreparedinsert"); // Execute statement with parameters sr = SQLExecute(hstmt); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in SQLExecute in OnViewTestpreparedinsert"); SQLFreeHandle(SQL_HANDLE_STMT, hstmt); } ................................................................... Ive Also Tried ........... void CInsert::OnBnClickedOk() { // TODO: Add your control notification handler code here OnOK(); SQLRETURN sr; //Take the values entered in edit box to CEdit m_First.GetWindowText(Firstname); m_Last.GetWindowText(Lastname); m_Add.GetWindowText(Address); CString FullName = "INSERT INTO Contacts (FirstName, LastName, Address) V

    CPalliniC 1 Reply Last reply
    0
    • A anna mathew

      thank you.... i tried it..but a new error comes up error C2594: 'static_cast' : ambiguous conversions from 'void (__thiscall CInsert::* )(void)' to 'AFX_PMSG'

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #12

      I haven't understood yet if you're calling a CDatabase method or not, because I cannot find a ExecuteSQL function in ODBC API. If you're using MFC to do database access then you don't need SQLCHAR data type. Could you please post a more complete code snippet (don't forget to format it using 'code block' button)? Could you you please explain better the scenario and your requirements?

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • R Rajesh R Subramanian

        anna mathew wrote:

        i tried it..but a new error comes up

        You tried what? Why don't you just show the code and be precise in asking questions? Do we need to ask you 10 questions just to get to know your exact problem?

        It is a crappy thing, but it's life -^ Carlo Pallini

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #13

        Rajesh R Subramanian wrote:

        You tried what? Why don't you just show the code and be precise in asking questions? Do we need to ask you 10 questions just to get to know your exact problem?

        Ya sir, but plz plz help, u r aware: your signature says it all :-D

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • A anna mathew

          Ive Edited My Code To: void CInsert::OnBnClickedOk() { OnOK(); SQLRETURN sr; SQLHSTMT hstmt; SQLCHAR SQL[] ="INSERT INTO Contacts (FirstName, LastName, Address) VALUES('?','?','?')"; //Take the values entered in edit box to CEdit * m_First.GetWindowText(Firstname); m_Last.GetWindowText(Lastname); m_Add.GetWindowText(Address); SQLINTEGER FirstNameLength = SQL_NTS; SQLINTEGER AddressLength = SQL_NTS; SQLINTEGER LastNameLength = SQL_NTS; SQLINTEGER FullLength = SQL_NTS; // Allocate a new statement handle sr = SQLAllocHandle(SQL_HANDLE_STMT, hDbConn, &hstmt); // Prepare statement if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in SQLAllocHandle in OnViewTestpreparedinsert"); sr = SQLPrepare(hstmt, SQL, SQL_NTS); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in SQLPrepare in OnViewTestpreparedinsert"); // Bind Parameters sr = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 10, 0, &Firstname, sizeof(Firstname), &FirstNameLength); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in Binding 2 in OnViewTestpreparedinsert"); sr = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 10, 0, &Lastname, sizeof(Lastname), &LastNameLength); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in Binding 3 in OnViewTestpreparedinsert"); sr = SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT,SQL_C_CHAR, SQL_CHAR, 10, 0, &Address, sizeof(Address), &AddressLength); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in Binding 3 in OnViewTestpreparedinsert"); // Execute statement with parameters sr = SQLExecute(hstmt); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in SQLExecute in OnViewTestpreparedinsert"); SQLFreeHandle(SQL_HANDLE_STMT, hstmt); } ................................................................... Ive Also Tried ........... void CInsert::OnBnClickedOk() { // TODO: Add your control notification handler code here OnOK(); SQLRETURN sr; //Take the values entered in edit box to CEdit m_First.GetWindowText(Firstname); m_Last.GetWindowText(Lastname); m_Add.GetWindowText(Address); CString FullName = "INSERT INTO Contacts (FirstName, LastName, Address) V

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #14

          anna mathew wrote:

          sr = SQLExecute(hstmt);

          Doesn't it work? What error do you get?

          anna mathew wrote:

          executeSQL((SQLCHAR *) (LPCTSTR)FullName);

          My previous suggestion was a crap, please ignore it (I've deleted) I don't know any executeSQL function. From where have you got it? BTW please answer to our questions to help us to help you :rolleyes: :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          A 1 Reply Last reply
          0
          • CPalliniC CPallini

            anna mathew wrote:

            sr = SQLExecute(hstmt);

            Doesn't it work? What error do you get?

            anna mathew wrote:

            executeSQL((SQLCHAR *) (LPCTSTR)FullName);

            My previous suggestion was a crap, please ignore it (I've deleted) I don't know any executeSQL function. From where have you got it? BTW please answer to our questions to help us to help you :rolleyes: :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            A Offline
            A Offline
            anna mathew
            wrote on last edited by
            #15

            Ive Reffered InformIT C++ 6 Unleashed to do database programming...... Ive not used CDatabase...... In My Program ...Initially Thr Is A Menu Bar With Options... Connect...Insert....Delete...and View... on Clicking Connect I get Connected to Database... all the other options can be pressed only after this... initially my code was working..... in Insert,Delete functions i had given only one statement... that is : executeSQL((SQLCHAR *) "INSERT INTO Contacts (Firstname,Lastname,Address) values ('MIKE', 'C', 'India'"); //in delete executeSQL((SQLCHAR *) "DELETE FROM Contacts WHERE FirstName ='MIKE'"); //code executeSQL void CMsQueryView::executeSQL (SQLCHAR *SQL) { SQLRETURN sr; //Return code for your ODBC calls SQLHSTMT hstmt; // Allocate new Statement Handle based on previous connection sr = SQLAllocHandle(SQL_HANDLE_STMT, hDbConn, &hstmt); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) { char message[200]; sprintf (message, "Error Allocating Handle: %d\n", sr); AfxMessageBox(message); } sr = SQLExecDirect(hstmt, SQL, SQL_NTS); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) { char message[200]; sprintf (message, "Error in SQLExecDirect. SQL was:\n\n%s\n\n", SQL); displayODBCError(sr, message); } SQLFreeHandle(SQL_HANDLE_STMT, hstmt); } //odbc error function void CMsQueryView::displayODBCError (SQLRETURN sr,char *inMessage) { if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) { SQLCHAR SqlState[6]; SQLINTEGER NativeError; SQLCHAR ErrMsg[SQL_MAX_MESSAGE_LENGTH]; int i = 1; char message[512]; strcpy (message, ""); if (inMessage) { strcpy(message, inMessage); strcat(message, " — "); } sprintf(message, "%sError in SQLConnect(): %d.", message, sr); AfxMessageBox(message); while(SQLGetDiagRec(SQL_HANDLE_DBC, hDbConn, i,SqlState, &NativeError, ErrMsg, sizeof(ErrMsg), NULL)!= SQL_NO_DATA) { sprintf(message,"Diag: %d, SQLSTATE: %s NativeError: %d ErrMsg: %s", i++, SqlState, NativeError, ErrMsg); AfxMessageBox(message); } } } ........................................ then it worked..... now i

            A CPalliniC 2 Replies Last reply
            0
            • A anna mathew

              Ive Reffered InformIT C++ 6 Unleashed to do database programming...... Ive not used CDatabase...... In My Program ...Initially Thr Is A Menu Bar With Options... Connect...Insert....Delete...and View... on Clicking Connect I get Connected to Database... all the other options can be pressed only after this... initially my code was working..... in Insert,Delete functions i had given only one statement... that is : executeSQL((SQLCHAR *) "INSERT INTO Contacts (Firstname,Lastname,Address) values ('MIKE', 'C', 'India'"); //in delete executeSQL((SQLCHAR *) "DELETE FROM Contacts WHERE FirstName ='MIKE'"); //code executeSQL void CMsQueryView::executeSQL (SQLCHAR *SQL) { SQLRETURN sr; //Return code for your ODBC calls SQLHSTMT hstmt; // Allocate new Statement Handle based on previous connection sr = SQLAllocHandle(SQL_HANDLE_STMT, hDbConn, &hstmt); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) { char message[200]; sprintf (message, "Error Allocating Handle: %d\n", sr); AfxMessageBox(message); } sr = SQLExecDirect(hstmt, SQL, SQL_NTS); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) { char message[200]; sprintf (message, "Error in SQLExecDirect. SQL was:\n\n%s\n\n", SQL); displayODBCError(sr, message); } SQLFreeHandle(SQL_HANDLE_STMT, hstmt); } //odbc error function void CMsQueryView::displayODBCError (SQLRETURN sr,char *inMessage) { if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) { SQLCHAR SqlState[6]; SQLINTEGER NativeError; SQLCHAR ErrMsg[SQL_MAX_MESSAGE_LENGTH]; int i = 1; char message[512]; strcpy (message, ""); if (inMessage) { strcpy(message, inMessage); strcat(message, " — "); } sprintf(message, "%sError in SQLConnect(): %d.", message, sr); AfxMessageBox(message); while(SQLGetDiagRec(SQL_HANDLE_DBC, hDbConn, i,SqlState, &NativeError, ErrMsg, sizeof(ErrMsg), NULL)!= SQL_NO_DATA) { sprintf(message,"Diag: %d, SQLSTATE: %s NativeError: %d ErrMsg: %s", i++, SqlState, NativeError, ErrMsg); AfxMessageBox(message); } } } ........................................ then it worked..... now i

              A Offline
              A Offline
              anna mathew
              wrote on last edited by
              #16

              I dint know database programming in MFC.... So I reffered the book and followedthe code... if there is much easier way of Doing It Then DO SUGGEST... Im Using ODBC and ACCESS database

              1 Reply Last reply
              0
              • CPalliniC CPallini

                Why do you need SQLCHAR? Aren't you using CDatabase? :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                modified on Thursday, October 23, 2008 3:55 AM

                A Offline
                A Offline
                anna mathew
                wrote on last edited by
                #17

                now im using CDatabase.... and modified code to : void CInsert::OnBnClickedOk() { OnOK(); SQLRETURN sr; SQLHSTMT hstmt; //SQLCHAR SQL[] ="INSERT INTO Contacts (FirstName, LastName, Address) VALUES('?','?','?')"; //Take the values entered in edit box to CEdit * m_First.GetWindowText(Firstname); m_Last.GetWindowText(Lastname); m_Add.GetWindowText(Address); CString Full ="INSERT INTO Contacts (FirstName, LastName, Address) VALUES ('" +Firstname+ "', '" +Lastname+"','" + Address+"'))"; SQLINTEGER FirstNameLength = SQL_NTS; sr = ExecuteSQL(Full); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) displayODBCError(sr,"Error in ExecuteSQL"); SQLFreeHandle(SQL_HANDLE_STMT, hstmt); } ................ still now same error......... error C2594: 'static_cast' : ambiguous conversions from 'void (__thiscall CInsert::* )(void)' to 'AFX_PMSG'

                1 Reply Last reply
                0
                • A anna mathew

                  Ive Reffered InformIT C++ 6 Unleashed to do database programming...... Ive not used CDatabase...... In My Program ...Initially Thr Is A Menu Bar With Options... Connect...Insert....Delete...and View... on Clicking Connect I get Connected to Database... all the other options can be pressed only after this... initially my code was working..... in Insert,Delete functions i had given only one statement... that is : executeSQL((SQLCHAR *) "INSERT INTO Contacts (Firstname,Lastname,Address) values ('MIKE', 'C', 'India'"); //in delete executeSQL((SQLCHAR *) "DELETE FROM Contacts WHERE FirstName ='MIKE'"); //code executeSQL void CMsQueryView::executeSQL (SQLCHAR *SQL) { SQLRETURN sr; //Return code for your ODBC calls SQLHSTMT hstmt; // Allocate new Statement Handle based on previous connection sr = SQLAllocHandle(SQL_HANDLE_STMT, hDbConn, &hstmt); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) { char message[200]; sprintf (message, "Error Allocating Handle: %d\n", sr); AfxMessageBox(message); } sr = SQLExecDirect(hstmt, SQL, SQL_NTS); if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) { char message[200]; sprintf (message, "Error in SQLExecDirect. SQL was:\n\n%s\n\n", SQL); displayODBCError(sr, message); } SQLFreeHandle(SQL_HANDLE_STMT, hstmt); } //odbc error function void CMsQueryView::displayODBCError (SQLRETURN sr,char *inMessage) { if(sr != SQL_SUCCESS && sr != SQL_SUCCESS_WITH_INFO) { SQLCHAR SqlState[6]; SQLINTEGER NativeError; SQLCHAR ErrMsg[SQL_MAX_MESSAGE_LENGTH]; int i = 1; char message[512]; strcpy (message, ""); if (inMessage) { strcpy(message, inMessage); strcat(message, " — "); } sprintf(message, "%sError in SQLConnect(): %d.", message, sr); AfxMessageBox(message); while(SQLGetDiagRec(SQL_HANDLE_DBC, hDbConn, i,SqlState, &NativeError, ErrMsg, sizeof(ErrMsg), NULL)!= SQL_NO_DATA) { sprintf(message,"Diag: %d, SQLSTATE: %s NativeError: %d ErrMsg: %s", i++, SqlState, NativeError, ErrMsg); AfxMessageBox(message); } } } ........................................ then it worked..... now i

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #18

                  Wow, finally some light in the darkness! If you are building a UNICODE application (Visula Studio 2003 and above default) then the CString object represent wide characters string, while SQLCHAR * is still a ANSI (C-like) string. you may use the macro CT2A for the required conversion (you have to include atlconv.h in you source file), for instance:

                  #include <atlconv.h>

                  //...

                  CT2A sqlFullName(FullName);
                  executeSQL((SQLCHAR *) (LPSTR) sqlFullName);

                  [added] you may also avoid string conversions using CStringA class, for instance:

                  CStringA szQuery = "SELECT * FROM MYTABLE";
                  executeSQL((SQLCHAR *) (LPCSTR) szQuery);

                  [/added] :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  In testa che avete, signor di Ceprano?

                  A 1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    Wow, finally some light in the darkness! If you are building a UNICODE application (Visula Studio 2003 and above default) then the CString object represent wide characters string, while SQLCHAR * is still a ANSI (C-like) string. you may use the macro CT2A for the required conversion (you have to include atlconv.h in you source file), for instance:

                    #include <atlconv.h>

                    //...

                    CT2A sqlFullName(FullName);
                    executeSQL((SQLCHAR *) (LPSTR) sqlFullName);

                    [added] you may also avoid string conversions using CStringA class, for instance:

                    CStringA szQuery = "SELECT * FROM MYTABLE";
                    executeSQL((SQLCHAR *) (LPCSTR) szQuery);

                    [/added] :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    A Offline
                    A Offline
                    anna mathew
                    wrote on last edited by
                    #19

                    Thank you so much......

                    CPalliniC 1 Reply Last reply
                    0
                    • A anna mathew

                      Thank you so much......

                      CPalliniC Offline
                      CPalliniC Offline
                      CPallini
                      wrote on last edited by
                      #20

                      You are welcome. :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      In testa che avete, signor di Ceprano?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups