gpt4 book ai didi

mysql - 使用 MyDAC 执行 proc

转载 作者:行者123 更新时间:2023-11-29 14:48:44 24 4
gpt4 key购买 nike

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

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

拜托,我需要你的帮助!我需要一个在 c++ builder 中执行存储过程的简单示例也欢迎链接!

最佳答案

以下是使用 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 组件来执行存储过程。

关于mysql - 使用 MyDAC 执行 proc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6248436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com