0

我在使用 c++ builder 2010 中的 dac 组件在 MySQL 服务器上执行简单过程时遇到问题。

我在这里找到了 delphi 的一个例子,http://stackoverflow.com/questions/3704173/return-value-of-stored-functions-in-mydac但我喜欢在 c++ builder 中看到一个例子

拜托我需要你的帮忙!我需要一个在 c++ 构建器中执行存储过程的简单示例,也欢迎使用链接!

4

1 回答 1

1

以下是使用 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));
}

您可以通过以下方式使用 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));
}

但我们建议您使用 TMyStoredProc 组件来执行存储过程。

于 2011-06-06T15:05:26.617 回答