0

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!

Suhrob Samiev
  • 1,528
  • 1
  • 25
  • 57

1 Answers1

1

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.

Devart
  • 119,203
  • 23
  • 166
  • 186
  • Thanks for your working example!! In addition I'd like to ask you one more question ... Is it possible to call stored procedure using generated sql statements like cal proc_name(par1,par2) using query comp or do I need to loop in your example??? Thanks in advance! – Suhrob Samiev Jun 08 '11 at 05:19
  • We've added an answer to the question about using query component for executing stored procedures. – Devart Jun 17 '11 at 11:50