简体   繁体   English

使用 MyDAC 执行 proc

[英]Execute proc using MyDAC

I am having trouble on executing simple procedure on MySQL server using my dac components in c++ builder 2010.我在 c++ builder 2010 中使用我的 dac 组件在 MySQL 服务器上执行简单程序时遇到问题。

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我在这里http://stackoverflow.com/questions/3704173/return-value-of-stored-functions-in-mydac中找到了一个示例 delphi 但我喜欢在 http://stackoverflow.com/questions/3704173/return-value-of-stored-builders-in-mydac 中看到一个示例,但我喜欢在 http://stackoverflow.com/questions/3704173/return-value-of-stored-builders-in-mydac 中看到一个示例

Please,I need your help!拜托我需要你的帮忙! I need a simple example of executing stored proc in c++ builder links are also welcome!我需要一个在 c++ 构建器链接中执行存储过程的简单示例也欢迎!

Here is an example of stored procedures execution using MyDAC:以下是使用 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:您可以通过以下方式使用 TMyQuery 组件执行存储过程:

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.但我们建议您使用 TMyStoredProc 组件来执行存储过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM