ActiveX Data Objects (ADO) enables you to write a client application to access and manipulate data in a database server through a provider.
ADO's primary benefits are ease of use, high speed, low memory overhead, and a small disk footprint.
This sample project is for ADODB, an implementation of ADO optimized for use with Microsoft OLE DB providers, including the Microsoft ODBC provider for OLE DB.
Using this we can execute stored procedure, pass arguments and retrieve value. To use this sample you will have to create the two stored procedures mentioned below.
For using this project you need MFC 5.0 OR above + ADO in your machine.
{
CString strTmp;
CString m_sdatasource; // Data source name
CString m_sUserID; // User Id
CString m_sPassword; // Password
// GET the above values from the user
//Without creating Datasource we can use database by the following code
/* strTmp.Format( "driver={sql server};"
"server=%s;"
"Database=%s;""UID=%s;""PWD=%s;",
m_server,m_sdatabase,m_sUserID,m_sPassword );*/
strTmp.Format( "dsn=%s;""UID=%s;""PWD=%s;",m_sdatasource,m_sUserID,m_sPassword );
_bstr_t bstrSQLServerConnect;
_bstr_t bstrProc =( L"sp_StartByteImport" );; //Stored procedure name
_variant_t Final;
bstrSQLServerConnect = (LPCTSTR) strTmp;
m_status="Empty File";
_ConnectionPtr Conn1; // connection object pointer
_CommandPtr Cmd1; // command object pointer
_RecordsetPtr Rs1; // recordset object pointer
bool bvalid = false;
try
{
Conn1.CreateInstance( __uuidof( Connection ) ); // Instantiating connection object
Conn1->ConnectionString = bstrSQLServerConnect; // giving the sqlconnection
Conn1->Open( bstrEmpty, bstrEmpty, bstrEmpty ); // open the connection object
Cmd1.CreateInstance( __uuidof( Command ) ); // creating command object
Cmd1->ActiveConnection = Conn1; // giving the connection handle
Cmd1->CommandText = _bstr_t( bstrProc ); // passing the stored procedue
Cmd1->CommandType = adCmdStoredProc; // type
Cmd1->Parameters->Refresh(); // passing string value as argument to stored procedure
Cmd1->Parameters->Item[ _variant_t( (long) 1 ) ]->Value = _variant_t( (LPCTSTR)m_sfilename );
Rs1 = Cmd1->Execute( &vtEmpty, &vtEmpty2, adCmdUnknown ); // executing the stored procedure and storing the recordset value
bvalid = true;
Final = Rs1->Fields->GetItem( _variant_t( 0L ) )->Value; // getting the first column value of the result row
strTmp.Format( "%s", CrackStrVariant( Final) ); // to see the value
// put your code to see all column values
}
catch( CException *e ) // trapping all error messages
{
TCHAR szCause[255];
e->GetErrorMessage(szCause, 255);
m_status=szCause;
}
catch( _com_error &e )
{
m_status=e.ErrorMessage( );
}
catch(...)
{
m_status="Error while executing the Import";
}
//we need to create the stored procedures below before running the application
//CREATE PROCEDURE sp_AddAccountingInfo @nfinal int, @pcDate datetime,
//@pcURL varchar (250), @pcTop varchar (250),
//@pcQueryString varchar (250), @pcBytes int, @pcRequests int AS
/*
Do your operation here
*/
//CREATE PROCEDURE sp_AddAccountingInfo
//@nfinal int,
//@pcDate datetime,
//@pcURL varchar (250),
//@pcTop varchar (250),
//@pcQueryString varchar (250),
//@pcBytes int,
//@pcRequests int
//AS
/*
Put your code here
*/
}
vc下用ado调用存储过程
1 _ConnectionPtr m_pConnection;
2 _CommandPtr m_pCommand;
.cpp中在函数中执行
//建立ado连接
3 HRESULT hr;
4 hr=m_pConnection.CreateInstance(__uuidof(Connection));
5 try
6 {
7 if(SUCCEEDED(hr))
8 {
9 hr=m_pConnection->Open(_bstr_t(L"Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Viper;Data Source=Viper"),_bstr_t (L"sa"),_bstr_t (L""),adModeUnknown);
10 }
11 }
12 catch(_com_error & err)
13 {
14 AfxMessageBox(err.Description(),MB_OK,0);
15 AfxMessageBox(err.ErrorMessage(),MB_OK,0);
16 AfxMessageBox("无法连接SQL SERVER 服务器,程序将退出。请检查网络设备",MB_OK,0);
17 exit(0);
18 }
//执行储存过程
19 CString cvar1,cvar2;
20 int cvar3;
21 cvar1="ddd";
22 cvar2="";
23 cvar3=0;
24 try
25 {
26 m_pCommand.CreateInstance(__uuidof(Command));
27 m_pCommand->ActiveConnection=app->m_pConnection;
28 m_pCommand->CommandType=adCmdStoredProc;
29 m_pCommand->CommandText=_bstr_t("pr_zs_dzdy");
30
31 _variant_t vvar1,vvar2,vvar3;
32 vvar1=_variant_t(_bstr_t(cvar1));
33 vvar2=_variant_t(_bstr_t(cvar2));
34 vvar3=_variant_t(cvar3);
35 _ParameterPtr mp_var1,mp_var2,mp_var3;
36 mp_var1.CreateInstance(__uuidof(Parameter));
37 mp_var2.CreateInstance(__uuidof(Parameter));
38 mp_var3.CreateInstance(__uuidof(Parameter));
39 mp_var1=m_pCommand->CreateParameter
40 (
41 _bstr_t("var1"),
42 adVarChar,
43 adParamInput,
44 3,
45 vvar1
46 );
47 m_pCommand->Parameters->Append(mp_var1);
48
49 mp_var2=m_pCommand->CreateParameter
50 (
51 _bstr_t("var2"),
52 adVarChar,
53 adParamOutput,
54 3,
55 vvar2
56 );
57 m_pCommand->Parameters->Append(mp_var2);
58
59 mp_var3=m_pCommand->CreateParameter
60 (
61 _bstr_t("var3"),
62 adIntger,
63 adParamOutput,
64 9,
65 vvar3
66 );
67 m_pCommand->Parameters->Append(mp_var3);
68
69
70 _variant_t vNull;
71 vNull.vt=VT_ERROR;
72 vNull.scode=DISP_E_PARAMNOTFOUND;
73 m_pCommand->Execute(&vNull,&vNull,adCmdStoredProc);
74 cvar2=mp_var2->Value.bstrVal;
75 cvar3=mp_var3->Value;
76 }
77 catch(_com_error &error)
78 {
79 MessageBox(error.ErrorMessage(),"ADO错误!");
80 MessageBox(error.Description(),"ADO错误!");
81 }
【打印文档】【大 中 小】【关闭窗口】
上一篇文章: VC Studio 使用技巧大全 2.0版本
下一篇文章: VC里一些容易混淆的地方
关于存储过程的ADO调用的一些心得(输出参数,返回值)
[本页面推荐在1024x768分辩率下浏览]
文章类别:数据库开发
网站目录: 网站首页 —> 数据库开发
转载自:www.csdn.net
在一个项目中,我需要用到存储过程来访问数据,为了提供一个比较一致的接口以便调用,我没有使用CreateParameter(),而是调用CommandPtr的Refresh()函数先从数据库中查询参数.
_ConnectionPtr m_pConn;
m_pConn.CreateInstance(__uuidof(Connection));
m_pConn->Open("driver={SQL Server};server=127.0.0.1;DATABASE=pub;UID=sa;PWD=", "","",0);
_CommandPtr m_pCommand;
m_pCommand.CreateInstance(__uuidof(Command));
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pCommand->ActiveConnection = m_pConn;
m_pCommand->CommandText = "SP_XX";//存储过程名
m_pCommand->PutCommandType(adCmdStoredProc);
m_pCommand->Parameters->Refresh();//从数据库查询参数信息
接下来就可以对每一个参数赋值了:
long cnt = m_pCommand->Parameters->GetCount();//取得参数的个数
for(long k=1;k<cnt;k++)
{//由于ADO中认为返回值是第一个参数即k=0时是返回值,因此这里用k=1滤掉第一个参数
m_pCommand->Parameters->GetItem(k)->Value = XXX;//按存储过程的参数顺序给参数赋值
}
现在可以执行这个存储过程了
m_pRecordset = m_pCommand->Execute(0,0,adCmdStoredProc);
这个时候,如果接下来用
_variant_t ret_val = m_pCommand->Parameters->GetItem((long)0)->Value;
那么将得不到值
而如果像下面这样调用的话就可以得到返回值了
m_pRecordset->Close();
_variant_t output_para = m_pCommand->Parameters->GetItem((long)0)->Value;
MS ADO.net给这一现象的回复是:
You can think of a stored procedure as a function in your code. The function doesn’t return a value until it has executed all of its code. If the stored procedure returns results and you haven’t finished processing these result
昨天做项目时发现此处不正确,m_pRecordset不能close。而且释放指针时要先释放m_pCommand,再释放m_pRecordset