Tuesday, March 18, 2008

Calling Sybase Stored Procedures through ADO

A few days ago I downloaded evaluation version of Sybase Adaptive Server Enterprise. The database is popularly known as Sybase. Here is some brief history of the database, and here is another very interesting thread on how Microsoft adapted Sybase as SQL Server.

Coming back to the topic intended for this post, below is a very small stored procedure which does almost nothing interesting (this, however, requires you to create a table named Test_Account with at least two integer columns, namely AccountId and Balance):

create procedure dbo.sp_getBalance
@pAccountId integer,
@oBalance integer output
as
begin
select @oBalance = Balance
from Test_Account where AccountId = @pAccountId
end


Calling this from Visual Basic is very simple. Below code has been tested with Sybase ASE OLEDB Provider:

Set Connect = New ADODB.Connection
Connect.CursorLocation = adUseServer
Connect.Provider = "ASEOLEDB"
Connect.ConnectionString = "Data Source=jaywalker:5000;Database=SybaseSPs;UID=sa;PWD=;"
Connect.Open

sqlString = "sp_getBalance"
Dim cmd As New ADODB.Command
cmd.CommandText = sqlString
cmd.CommandType = adCmdStoredProc
Set cmd.ActiveConnection = Connect

Call cmd.Parameters.Append(cmd.CreateParameter("@pAccountId", adInteger, adParamInput, 4, 3))
Call cmd.Parameters.Append(cmd.CreateParameter("@oBalance", adInteger, adParamOutput))
cmd.Execute

MsgBox cmd.Parameters("@oBalance").Value


The same can be coded (without exception handling) in VC++ as follows:

CoInitialize(NULL);
_CommandPtr sybCmd ("ADODB.Command");
_ConnectionPtr sybConn ("ADODB.Connection");

sybConn->CursorLocation = adUseServer;
sybConn->Provider = "ASEOLEDB";
_bstr_t connString = "Data Source=jaywalker:5000;Database=SybaseSPs;UID=sa;PWD=;";
sybConn->Open (connString, "", "", adConnectUnspecified);

bstr_t sql = "sp_getBalance";
sybCmd->ActiveConnection = sybConn;
sybCmd->CommandType = adCmdStoredProc;
sybCmd->CommandText = sql;

sybCmd->Parameters->Append (sybCmd->CreateParameter ("@pAccountId", adInteger, adParamInput, sizeof(int), 3));
sybCmd->Parameters->Append (sybCmd->CreateParameter ("@oBalance", adInteger, adParamOutput, sizeof(int)));

sybCmd->Execute(NULL, NULL, adExecuteNoRecords);

long val = sybCmd->Parameters->Item["@oBalance"]->Value;

CoUninitialize();




Years ago, I wrote a similar article on calling Oracle Stored Procedures through ADO from both Visual Basic 6 and VC++ 6. That page became quite popular with the passage of time.

1 comments:

Anonymous said...

Hey,

When ever I surf on web I come to this website[url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips].[/url]Plenty of useful information on blog.thejaywalker.net. I am sure due to busy scedules we really do not get time to care about our health. Are you really serious about your weight?. Recent Research points that almost 90% of all USA adults are either fat or overweight[url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips].[/url] Therefore if you're one of these individuals, you're not alone. Its true that we all can't be like Brad Pitt, Angelina Jolie, Megan Fox, and have sexy and perfect six pack abs. Now the question is how you are planning to have quick weight loss? Quick weight loss can be achived with little effort. If you improve some of your daily diet habbits then, its like piece of cake to quickly lose weight.

About me: I am webmaster of [url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips]Quick weight loss tips[/url]. I am also mentor who can help you lose weight quickly. If you do not want to go under difficult training program than you may also try [url=http://www.weightrapidloss.com/acai-berry-for-quick-weight-loss]Acai Berry[/url] or [url=http://www.weightrapidloss.com/colon-cleanse-for-weight-loss]Colon Cleansing[/url] for effective weight loss.

Post a Comment