Pubs 範例資料庫中建立下列的預存程序正在執行 Microsoft SQL Server 的電腦上:
Create Procedure TestProcedure
(
@au_idIN varchar (11),
@numTitlesOUT Integer OUTPUT
)
As
select A.au_fname, A.au_lname, T.title
from authors as A join titleauthor as TA on
A.au_id=TA.au_id
join titles as T
on T.title_id=TA.title_id
where A.au_id=@au_idIN
set @numTitlesOUT = @@Rowcount
return (5)
啟動 Microsoft Visual Studio.NET,並在 Visual C++.NET 中建立新的受管理的 C + + 應用程式專案。
#include "stdafx.h"
#using <mscorlib.dll>
#include <tchar.h>
#using <system.dll>
using namespace System;
#using <system.data.dll>
using namespace System::Data;
using namespace System::Data::SqlClient;
// This is the entry point for this application.
int _tmain(void)
{
try{
SqlConnection *myCon = new SqlConnection("Data Source=mySQLServer;User ID=<username>;
Password=<strong password>;initial catalog=pubs;");
myCon->Open();
SqlCommand *myCmd = new SqlCommand("TestProcedure", myCon);
myCmd->CommandType = CommandType::StoredProcedure;
// Parameter order does not matter because SqlClient supports named
// parameters. Type the exact parameter names as declared in the
// stored procedure definition at the backend.
// Parameters should be prefixed with @.
myCmd->Parameters->Add("@au_idIN",SqlDbType::VarChar,11);
myCmd->Parameters->get_Item(0)->Value=S"213-46-8915";
SqlParameter *retParam;
retParam=myCmd->Parameters->Add("RetVal",SqlDbType::Int,4);
retParam->Direction=ParameterDirection::ReturnValue;
SqlParameter *outParam;
outParam=myCmd->Parameters->Add("@numTitlesOUT",SqlDbType::Int,4);
outParam->Direction=ParameterDirection::Output;
SqlDataReader *myReader;
myReader=myCmd->ExecuteReader();
while(myReader->Read())
{
for(int col=0;col<myReader->FieldCount;col++)
{
Console::Write("{0}: {1}",(myReader->GetName(col))->ToString(),
(myReader->GetValue(col))->ToString());
Console::WriteLine();
}
Console::WriteLine();
}
myReader->Close();
Console::WriteLine("Output Param:{0}; Return Value:{1}",
outParam->Value,retParam->Value);
myCon->Close();
}
catch(SqlException *mySqlEx)
{
for(int i=0;i<mySqlEx->Errors->Count;i++)
{
Console::WriteLine("Source={0};Message={1};",mySqlEx->Errors->
Item[i]->Source,mySqlEx->Errors->Item[i]->Message);
}
}
catch(System::Exception *ex)
{
Console::WriteLine(ex->get_Message());
}
}
OLE DB 資料提供者
#include "stdafx.h"
#using <mscorlib.dll>
#include <tchar.h>
#using <system.dll>
using namespace System;
#using <system.data.dll>
using namespace System::Data;
using namespace System::Data::OleDb;
// This is the entry point for this application.
int _tmain(void)
{
try{
OleDbConnection *myCon = new OleDbConnection("Provider=SQLOLEDB.1;
Data Source=mySQLServer;User ID=<username>;
Password=<strong password>;initial catalog=pubs;");
myCon->Open();
OleDbCommand *myCmd = new OleDbCommand("TestProcedure", myCon);
myCmd->CommandType = CommandType::StoredProcedure;
// The following notation also works. To test this notation,
// comment out the above two lines, and uncomment the next two lines.
// OleDbCommand *myCmd = new OleDbCommand("{?=call TestProcedure(?,?)}", myCon);
// myCmd->CommandType = CommandType::Text;
// The parameter order is important in this sample. Parameters are
// matched with stored procedure parameters in the order they are supplied.
// Names can be anything. You do not have to prefix them with "@".
OleDbParameter* retParam=myCmd->Parameters->Add("RetVal",OleDbType::Integer,4);
retParam->Direction=ParameterDirection::ReturnValue;
myCmd->Parameters->Add("au_idIN",OleDbType::VarChar,11);
myCmd->Parameters->Item[1]->Value=S"213-46-8915";
OleDbParameter *outParam=myCmd->Parameters->Add("numTitlesOUT",OleDbType::Integer,4);
outParam->Direction=ParameterDirection::Output;
OleDbDataReader *myReader;
myReader=myCmd->ExecuteReader();
int rowCnt=0;
while(myReader->Read())
{
for(int col=0;col<myReader->FieldCount;col++)
{
Console::Write("{0}: {1}",(myReader->GetName(col))->ToString(),
(myReader->GetValue(col))->ToString());
Console::WriteLine();
}
Console::WriteLine();
rowCnt++;
}
myReader->Close();
Console::WriteLine("Output Param:{0}; Return Value:{1}",outParam->
Value,retParam->Value);
myCon->Close();
}
catch(OleDbException *mySqlEx)
{
for(int i=0;i<mySqlEx->Errors->Count;i++)
{
Console::WriteLine("Source={0};Message={1};",mySqlEx->Errors->
Item[i]->Source,mySqlEx->Errors->Item[i]->Message);
}
}
catch(System::Exception *ex)
{
Console::WriteLine(ex->get_Message());
}
}
Create Procedure TestProcedure2
(
@au_idIN varchar (11)
)
As
select count (T.title)
from authors as A join titleauthor as TA on
A.au_id=TA.au_id
join titles as T
on T.title_id=TA.title_id
where A.au_id=@au_idIN
Return(5)
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。