我試圖從用C++編寫的控制檯應用程序調用我的Microsoft SQL Server上的用戶定義函數。整個事情我都用ADO。如何使用ADO從C++調用用戶定義的函數
我的功能看起來像這樣(和它的作品時,我用它從Management Studio中)
CREATE FUNCTION [dbo].[funcHowOftenDidHeWin]
(
@playername as varchar(15)
@percentWon float OUTPUT
)
RETURNS float
AS
BEGIN
DECLARE @wintimes float
DECLARE @participated float
DECLARE @percentWon float
SELECT @wintimes = COUNT(DidWin)
FROM PPLP0p02
WHERE DidWin = 1 AND [email protected]
SELECT @participated = COUNT(DidWin)
FROM PPLP0p02
WHERE Playername = @playername
IF @wintimes >0
SET @percentWon = @wintimes/@participated *100
IF @wintimes = 0
SET @percentWon = 0
RETURN @percentWon
END
GO
從我的節目,我嘗試調用它,如下所示:
USEADO::_ConnectionPtr connection;
USEADO::_RecordsetPtr recordset; //create a "
if(FAILED(hr = CoInitialize(NULL))) //
{ return hr;}
if(FAILED(hr = connection.CreateInstance(__uuidof(USEADO::Connection))))
{ return hr;}
if(FAILED(hr = recordset.CreateInstance(__uuidof(USEADO::Recordset))))
{ return hr;}
connection->CursorLocation = USEADO::adUseServer;
try
{
connection->Open(L"Provider=SQLOLEDB.1;Persist Security Info=False;User ID=Test;Initial Catalog=Haufen;Data Source=Wolle\\SQLEXPRESS", L"Test", L"Test", USEADO::adConnectUnspecified);
}
catch(...) {std::cout << "!!! connection->Open(ConnectionString FAILED !!!" << std::endl; }
//This works
recordset->Open("SELECT playername FROM Summary WHERE playersatbegin=5",connection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
//This not
recordset->Open("SELECT dbo.funcHowOftenDidHeWin('Lumpi') ",connection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
好,這是行不通的。調試器以1580行的masado15.tli結尾,VS顯示一個窗口,其中包含以下錯誤:35h.exe中的0x7c812afb處未處理的異常:Microsoft C++異常:內存位置0x0012c3f8處的_com_error。
我不知道如果語法是正確的,我不知道該服務器應該如何從我想要的結果的表就知道....
任何幫助在那裏?謝謝
Lumpi
你有錯誤嗎?如果是這樣......將其粘貼在這裏。 – garnertb 2011-05-24 22:17:00
錯誤是:35h.exe中0x7c812afb未處理的異常:Microsoft C++異常:_com_error在內存位置0x0012c3f8 .. – Lumpi 2011-05-25 18:30:25