mysqlstored-proceduresmydac

Execute proc using MyDAC


I am having trouble on executing simple procedure on MySQL server using my dac components in c++ builder 2010.

I have found an example in delphi over here http://stackoverflow.com/questions/3704173/return-value-of-stored-functions-in-mydac but I like to see an example in c++ builder

Please,I need your help! I need a simple example of executing stored proc in c++ builder links are also welcome!


Solution

  • Here is an example of stored procedures execution using MyDAC:

    void __fastcall TForm1::BitBtn1Click(TObject *Sender) {
      TMyConnection* con = new TMyConnection(this);
      con->Server = "servername";
      con->Port = 3306;
      con->Username = "username";
      con->Password = "password";
      con->LoginPrompt = False;
      con->Database = "databasename";
    
      // you should comment this code after the first execution
      TMyQuery* qr = new TMyQuery(this);
      qr->Connection = con;
      qr->SQL->Clear();
      qr->SQL->Add("CREATE PROCEDURE SumTwoInts(IN Num1 INT, IN Num2 INT, OUT Num3 INT)");
      qr->SQL->Add("BEGIN");
      qr->SQL->Add("SET Num3 = Num1 + Num2;");
      qr->SQL->Add("END");
      qr->Execute();
    
      TMyStoredProc* sp = new TMyStoredProc(this);
      sp->Connection = con;
      sp->StoredProcName = "SumTwoInts";
      sp->PrepareSQL();
      sp->ParamByName("Num1")->AsInteger = 2;
      sp->ParamByName("Num2")->AsInteger = 3;
      sp->Execute();
      ShowMessage(IntToStr(sp->ParamByName("Num3")->AsInteger));
    }
    

    You can use the TMyQuery component to execute a stored procedure in the following way:

    void __fastcall TForm1::BitBtn1Click(TObject *Sender) {
    TMyConnection* con = new TMyConnection(this);
    con->Server = "servername";
    con->Port = 3306;
    con->Username = "username";
    con->Password = "password";
    con->LoginPrompt = False;
    con->Database = "databasename";
    
    TMyQuery* qr = new TMyQuery(this);
    qr->Connection = con;
    qr->SQL->Text = "CALL SumTwoInts(:Num1, :Num2, @Num3); SELECT CAST(@Num3 AS SIGNED) AS '@Num3'";
    qr->ParamByName("Num1")->AsInteger = 2;
    qr->ParamByName("Num2")->AsInteger = 3;
    qr->Open();
    ShowMessage(IntToStr(qr->Fields->Fields[0]->AsInteger));
    }
    

    But we recommend you to use the TMyStoredProc component for executing stored procedures.